By creating a class (Factory) that specializes in instantiation A design pattern that definitely creates relevant instances. In other words, an implementation in which the instances that can be created are determined for each Factory class.
Create a class (= Factory class) that creates a related class. ** Example): Tokyo and Japanese, Washington DC and English are related. ** **
Factory.java
public interface Factory {
Capital getCapital();
Language getLanguage();
}
JapanFactory.java
//Japanese Factory class
public class JapanFactory implements Factory {
@Override
public Capital getCapital() {
return new Tokyo(); //The capital is Tokyo
}
@Override
public Language getLanguage() {
return new Japanese(); //Language is Japanese
}
}
UsaFactory.java
//American Factory class
public class UsaFactory implements Factory {
@Override
public Capital getCapital() {
return new WashingtonDC(); //The capital is Washington DC
}
@Override
public Language getLanguage() {
return new English(); //Language is english
}
}
Create a class that inherits the abstract class that is the return value of the method implemented in the Factory interface. This class is the class that is actually returned by each Factory class. ** Example): The Tokyo class that inherits the Capital class and the WashingtonDC class are actually returned from the Factory class. ** **
Capital.java
public abstract class Capital {
//The fields here are not particularly relevant for the description of the Abstract Factory.
protected String name;
protected int population;
}
Tokyo.java
public class Tokyo extends Capital {
public Tokyo() {
this.name = "Tokyo";
this.population = 13510000;
}
}
WashingtonDC.java
public class WashingtonDC extends Capital {
public WashingtonDC() {
this.name = "Washington D.C";
this.population = 601723;
}
}
Implement the process to generate Factory class on the caller side. At this time, a branch to switch the Factory class is required.
Main.java
public class Main {
public static void main(String[] args) {
String name = "Japan";
Factory factory = createFactory(name);
Capital capital = factory.getCapital();
Language language = factory.getLanguage();
System.out.println("The capital name is" + capital.name);
System.out.println("Population" + capital.population);
}
private static Factory createFactory(String name) {
switch (name) {
case "Japan":
return new JapanFactory();
case "USA":
return new UsaFactory();
default:
throw new IllegalArgumentException();
}
}
}
Execution result
The capital name is Tokyo
Population is 13510000
By creating an instance via the Factory class, you can safely create a related instance. ** → By creating a Japan Factory instance, you can always create a Tokyo instance and a Japanese instance. ** ** (If you create an instance explicitly, you may create an instance with the wrong combination.)
Also, since the return value is acquired by the abstract class, the caller does not have to worry about the internal implementation of the Factory class. ** → You don't need to know what the implementation of the Tokyo class or Washington DC class is. ** **
Recommended Posts