[JAVA] Builder Pattern

A pattern in which the user unknowingly performs a complex program combination (composition)
A constructor that requires multiple constructors or has many combinations of arguments is programmed. Can also be used when externalizing </ font>

This page describes the Builder Pattern.

Design Pattarm MENU

Check with the following class structure

class Explanation
abstract Builder.class Define multiple methods
samBuilder1.class Builder.Implement class
samBuilder2.class the same
Director.class Compose multiple methods of Builder
user(Main.class) Check the operation
  • User Includes the meaning that other developers use this pattern

Builder.class


abstract class Builder{
  abstract void line1();
  abstract void line2();
  abstract void msg(String msg);
}

samBuilder1.class


class samBuilder1 extends Builder{
  void line1(){System.out.println("|||||||||||||");}
  void line2(){System.out.println(".............");}
  void msg(String msg){System.out.println(msg);}
}

samBuilder2.class


class samBuilder2 extends Builder{
  void line1(){System.out.println("=============");}
  void line2(){System.out.println("~~~~~~~~~~~~~");}
  void msg(String msg){System.out.println(msg);}
}

Director.class


class Director{
  Builder builder;
  Director (Builder builder){this.builder=builder;}

  void construct(String msg){  //Write complex constructors here or combine methods
    builder.line1();           // A
    builder.msg(msg);          // B
    builder.line2();           // C
  }                            // A~The output changes by changing the order of C. Example of building a method
}

user(Main.class)


public static void main(String[] args){
  samBuilder1 sam1 = new samBuilder1();
  Director dc1 = new Director(sam1);
  dc1.construct("Builder");

  samBuilder2 sam2 = new samBuilder2();
  Director dc2 = new Director(sam2);
  dc2.construct("builder");
}}

The user cannot see the concrete method of Builder

Recommended Posts

Builder pattern
Builder Pattern
Design pattern ~ Builder ~
Design pattern (2): Builder
Builder pattern (Effective Java)
Memento Pattern
Mediator pattern
Iterator pattern
Composite 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
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
Object Mother pattern and Test Data Builder pattern
abstract Factory Pattern
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
java callback pattern
Test Data Builder pattern ~ Improve maintainability of test code
Design pattern ~ Proxy ~
Design pattern ~ State ~
Factory Method Pattern
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern ~ Composite ~
Design pattern (1): AbstractFactory
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~