[JAVA] Design pattern (2): Builder

What is Builder?

When common processing is required between multiple instances, by hiding that processing An implementation that can make the internal processing different even for the same processing. Here, Japanese and Americans are created based on the People class, which has a field called mainLanguage.

Step ①

Create an interface that defines common processing between multiple instances. (= Builder class)

Builder.java


public interface Builder {
	void createMainLanguage(); //Set the main language
	People getResult(); //Return People instance
}

People.java


public class People {
    //subject
	private String mainLanguage;

	public String getMainLanguage() {
		return mainLanguage;
	}

	public void setMainLanguage(String mainLanguage) {
		this.mainLanguage = mainLanguage;
	}
	
	void hello() {
		System.out.println("Hello. The main language is" + mainLanguage);
	}
}

Step ②

Create a class with an implementation of the defined interface. (= ConcreteBuilder class) ** When the types of instances to be created increase, generate and increase this class **

JapaneseBuilder.java


//Japanese Builder
public class JapaneseBuilder implements Builder {
	private People people;
	
	public JapaneseBuilder() {
		this.people = new People();
	}

	@Override
	public void createMainLanguage() {
		people.setMainLanguage("Japanese"); //Japanese is the main language
	}

	@Override
	public People getResult() {
		return this.people;
	}
}

AmericanBuilder.java


//American Builder
public class AmericanBuilder implements Builder {
	private People people;
	
	public AmericanBuilder() {
		this.people = new People();
	}

	@Override
	public void createMainLanguage() {
		people.setMainLanguage("English"); //English is the main language
	}

	@Override
	public People getResult() {
		return this.people;
	}
}

Step ③

Create a class that is responsible for the actual instantiation. (= Director class) ** * Make sure to have the interface as an argument of the constructor ** By passing Builder to Director, it becomes independent of ConcreteBuilder class. In other words, Director is unaware of what the actual class, ConcreteBuilder, is.

Director.java


public class Director {
	private Builder builder;
	
	public Director(Builder builder) {
		this.builder = builder;
	}
	
	public People construct() {
		builder.createMainLanguage();
		return builder.getResult();
	}
}

How to call

(1) Create a builder instance that has a builder interface as a type. The actual processing depends on ConcreteBuilder at the time of generation. (2) Create a Director instance with the builder instance as an argument.

Main.java


public class Main {
	public static void main(String[] args) {
		if (args[0].equals("Japanese")) {
			//Generate Japanese builder
			Builder builder = new JapaneseBuilder();
			//The Director is just passing the Bulder interface, so I don't even know the internal implementation of the Builder.
			Director director = new Director(builder);
			People people = director.construct();
			people.hello();
		} else if (args[0].equals("English")) {
			Builder builder = new AmericanBuilder();
			Director director = new Director(builder);
			People people = director.construct();
			people.hello();
		} else {
			throw new IllegalArgumentException();
		}
	}
}

Recommended Posts

Design pattern ~ Builder ~
Design pattern (2): Builder
Builder pattern
Builder Pattern
Design pattern ~ Visitor ~
Java design pattern
Design pattern ~ Proxy ~
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Composite ~
Design pattern (1): AbstractFactory
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 ~
Builder pattern (Effective Java)
Java design pattern summary
Introduction to Design Patterns (Builder)
Design pattern ~ Chain of Responsibility ~
[Design pattern] Java core library
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
Ruby design pattern template method pattern memo
C # chewed design pattern: Template Method
Application example of design pattern (No. 1)
Java beginner design pattern (Factory Method pattern)
Prototype pattern
Memento Pattern
Mediator pattern
Iterator pattern
Composite pattern
Object Mother pattern and Test Data Builder pattern
Observer 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