Builder Mode)
Instance:
Painter
UML class diagram:
Instance implementation code:
abstract class PersonBuilder{ protected Graphics g; protected Pen p; public PersonBuilder(Graphics g, Pen p) { this.g = g; this.p = p; } public abstract void BuildHead(); public abstract void BuildBody(); public abstract void BuildArmLeft(); public abstract void BuildArmRight(); public abstract void BuildLegLeft(); public abstract void BuildLegRight();}class PersonThinBuilder : PersonBuilder{ public PersonThinBuilder(Graphics g, Pen p) : base(g, p) { } public override void BuildHead() { g.DrawEllipse(p, 50, 20, 30, 30); } public override void BuildBody() { g.DrawRectangle(p, 60, 50, 10, 50); } public override void BuildArmLeft() { g.DrawLine(p, 60, 50, 40, 100); } public override void BuildArmRight() { g.DrawLine(p, 70, 50, 90, 100); } public override void BuildLegLeft() { g.DrawLine(p, 60, 100, 45, 150); } public override void BuildLegRight() { g.DrawLine(p, 70, 100, 85, 150); }}class PersonFatBuilder : PersonBuilder{ public PersonFatBuilder(Graphics g, Pen p) : base(g, p) { } public override void BuildHead() { g.DrawEllipse(p, 50, 20, 30, 30); } public override void BuildBody() { g.DrawEllipse(p, 45, 50,40, 50); } public override void BuildArmLeft() { g.DrawLine(p, 50, 50, 30, 100); } public override void BuildArmRight() { g.DrawLine(p, 80, 50, 100, 100); } public override void BuildLegLeft() { g.DrawLine(p, 60, 100, 45, 150); } public override void BuildLegRight() { g.DrawLine(p, 70, 100, 85, 150); }}
class PersonDirector{ private PersonBuilder pb; public PersonDirector(PersonBuilder pb) { this.pb = pb; } public void CreatePerson() { pb.BuildHead(); pb.BuildBody(); pb.BuildArmLeft(); pb.BuildArmRight(); pb.BuildLegLeft(); pb.BuildLegRight(); }}
Client implementation:
Pen p = new Pen(Color.Yellow); PersonThinBuilder ptb = new PersonThinBuilder(pictureBox1.CreateGraphics(), p); PersonDirector pdThin = new PersonDirector(ptb); pdThin.CreatePerson(); PersonFatBuilder pfb = new PersonFatBuilder(pictureBox2.CreateGraphics(), p); PersonDirector pdFat = new PersonDirector(pfb); pdFat.CreatePerson();
Note:
Separates the construction of a complex object from its representation so that different representations can be created during the same construction process. Such a design pattern is called the builder pattern.
It is mainly used to create complex objects. The building sequence between these objects is stable, but the building inside the object is usually subject to complex changes.
For example, ConcreteBuilder and ConcreteBuilder internal building (building the head and building the body ...) they are all the same. Even if there is another tall person, a short person, and so on, the construction sequence and process remain unchanged, you can create a Builder (Director) to complete the work, the specific head and body creation details of each villain are different. The size of the body is larger and the length of the foot is longer. That is, the internal construction of the object is different. Therefore, we hand over the assembling of the villain to the builder, and the building of the villain's components is handed over to the villain.
When to use:
1. When creating complex objects, algorithms should be independent of the components of the objects and their assembly methods.
2. When the constructor must allow different representations of the constructed object.
In such a design model, there are several roles:
Builder:
Specify an abstract interface for each part of a product object.
ConcreteBuilder:
Implement the Builder interface to construct and assemble each part of the product, define and define the representation it creates, and provide an interface for retrieving the product.
Director:
Construct an object using the Builder interface.
Product:
Indicates a complex object to be constructed. ConcreteBuilder creates an internal representation of the product and defines its assembly process, including the class that defines the components, including the interfaces that assemble these components into the final product.