What is design patterns?
復習用に保存
復習用に保存
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
WithoutBook は、分野別の面接質問、オンライン練習テスト、チュートリアル、比較ガイドをひとつのレスポンシブな学習空間にまとめています。
Java Design Patterns の人気面接質問と回答を確認し、新卒者や経験者が就職面接の準備を進められます。
Java Design Patterns の人気面接質問と回答を確認し、新卒者や経験者が就職面接の準備を進められます。
質問を検索して回答を確認できます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
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.
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。
この項目をブックマークに追加したり、難しい内容としてマークしたり、復習セットに入れたりできます。