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