[JAVA] Design pattern (1): AbstractFactory

What is Abstract Factory?

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.

Step ①

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
	}
}

Step ②

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;
	}
}

Step ③

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

merit

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

Design pattern (1): AbstractFactory
Design pattern ~ Builder ~
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (2): Builder
Design pattern ~ Command ~
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Design pattern ~ Decorator ~
Design pattern ~ Interpreter ~
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
C ++ design pattern (TemplateMethod pattern)
Design pattern ~ Factory Method ~
Design pattern ~ Abstract Factory ~
GoF design pattern summary
Design pattern ~ Template Method ~
Java design pattern summary
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Strategy pattern
Iterator Pattern
Visitor pattern
Adapter Pattern
Proxy Pattern
Strategy Pattern
Composite Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
[Design pattern] Common logic with Template Method
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
PrintObserver "Observer Design Pattern: Description and Implementation Example"
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -