What is design patterns?
복습용 저장
복습용 저장
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
WithoutBook은 주제별 면접 질문, 온라인 연습 테스트, 튜토리얼, 비교 가이드를 하나의 반응형 학습 공간으로 제공합니다.
Know the top Java Design Patterns interview questions and answers for freshers and experienced candidates to prepare for job interviews.
Know the top Java Design Patterns interview questions and answers for freshers and experienced candidates to prepare for job interviews.
Search a question to view the answer.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
We're going to create a Mobile interface and concrete classes implementing the Mobile interface. A factory class MobileFactory is defined as a next step.
FactoryPatternDemo, our demo class will use MobileFactory to get a Mobile object. It will pass information (SAMSUNG / SONY / BLACKBERRY) to MobileFactory to get the type of object it needs.
Step 1: Create an interface.
Mobile.java
public interface Mobile {
void draw();
}
Step 2
Create concrete classes implementing the same interface.
Sony.java
public class Sony implements Mobile {
@Override
public void draw() {
System.out.println("Inside Sony::draw() method.");
}
}
Blackberry.java
public class Blackberry implements Mobile {
@Override
public void draw() {
System.out.println("Inside Blackberry::draw() method.");
}
}
Samsung.java
public class Samsung implements Mobile {
@Override
public void draw() {
System.out.println("Inside Samsung::draw() method.");
}
}
Step 3
Create a Factory to generate object of concrete class based on given information.
MobileFactory.java
public class MobileFactory {
//use getMobile method to get object of type Mobile
public Mobile getMobile(String MobileType){
if(MobileType == null){
return null;
}
if(MobileType.equalsIgnoreCase("SAMSUNG")){
return new Samsung();
} else if(MobileType.equalsIgnoreCase("SONY")){
return new Sony();
} else if(MobileType.equalsIgnoreCase("BLACKBERRY")){
return new Blackberry();
}
return null;
}
}
Step 4
Use the Factory to get object of concrete class by passing an information such as type.
FactoryPatternDemo.java
public class FactoryPatternDemo {
public static void main(String[] args) {
MobileFactory MobileFactory = new MobileFactory();
//get an object of Samsung and call its draw method.
Mobile Mobile1 = MobileFactory.getMobile("SAMSUNG");
//call draw method of Samsung
Mobile1.draw();
//get an object of Sony and call its draw method.
Mobile Mobile2 = MobileFactory.getMobile("SONY");
//call draw method of Sony
Mobile2.draw();
//get an object of Blackberry and call its draw method.
Mobile Mobile3 = MobileFactory.getMobile("BLACKBERRY");
//call draw method of Blackberry
Mobile3.draw();
}
}
Step 5
Verify the output.
Inside Samsung::draw() method.
Inside Sony::draw() method.
Inside Blackberry::draw() method.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.