This article summarizes the Abstract Factory. According to wikipedia, "Provides a way to properly generate a set of related instances depending on the situation." Reference: [Design pattern (software)](https://ja.wikipedia.org/wiki/%E3%83%87%E3%82%B6%E3%82%A4%E3%83%B3%E3%83 % 91% E3% 82% BF% E3% 83% BC% E3% 83% B3_ (% E3% 82% BD% E3% 83% 95% E3% 83% 88% E3% 82% A6% E3% 82% A7% E3% 82% A2)))
I referred to the following article. Contemplation: Abstract Factory pattern Abstract Factory for implementing and understanding GoF design patterns in Java
If you want to use the methods of another class from one class, you need to instantiate "new class name ()" except for the static class. In this design pattern, what was new to the calling class is moved to the class group called Factory, and the instance of the class you want to use is acquired via the method held by the Factory class.
Abstract Factory represents the case of refueling high octane and regular at a gas station.
NO | file name | Class contents |
---|---|---|
1 | AbstractFactory.java | An abstract class that defines refueling gasoline |
2 | ConcreteHighFactory.java | Concrete class to refuel high octane |
3 | ConcreteRegularFactory.java | Concrete class to refuel regular |
4 | AbstractRefueling.java | Refueling abstract class |
5 | ConcreteHighRefueling.java | High-octane refueling concrete class |
6 | ConcreteRegularRefueling.java | Regular refueling concrete class |
7 | Main.java | Class to execute |
Java
AbstractFactory.java
public abstract AbstractFactory {
abstract AbstractRefueling getAbstractRefueling();
}
ConcreteHighFactory.java
public class ConcreteHighFactory extends AbstractFactory {
@Override
public AbstractRefueling getAbstractRefueling();
return new ConcreteHighRefueling();
}
}
ConcreteRegularFactory.java
public class ConcreteRegularFactory extends AbstractFactory {
@Override
public AbstractRefueling getAbstractRefueling();
return new ConcreteRegularRefueling();
}
}
AbstractRefueling.java
public abstract class AbstractRefueling {
abstract payment();
abstract refueling();
}
ConcreteHighRefueling.java
public class ConcreteHighRefueling extends AbstractRefueling {
@Override
public payment() {};
@Override
public refueling() {};
}
ConcreteRegularRefueling.java
public class ConcreteRegularRefueling extends AbstractRefueling {
@Override
public payment() {};
@Override
public refueling() {};
}
Main.java
public class Main {
public static void main(String... args) {
Factory fa = exec("High");
AbstractRefueling ab = fa.getAbstractRefueling();
}
private static Factory exec(String env) {
if (env == "high") {
return new ConcreteHighFactory();
} else if (env == "regular") {
return new ConcreteRegularFactory();
}
}
}
As mentioned above, we are receiving a class to refuel high octane gasoline on the assumption that high octane gasoline will be added on the Main method. If necessary, you can replace it with other functions defined in the Factory class by rewriting Conclete ~ Factory in the exec method in Main.java. Also, in this case, we implemented two types, high-octane and regular, but if you need to add a processing type such as light oil, you can simply add a class because it is abstracted, and the existing one. It can be said that it has little effect on high-octane and regular classes. However, although it is flexible to add classes, when adding parts (in this example, adding a window cleaning method in addition to the refueling method), the range of influence extends to all Factory classes.
Recommended Posts