Builder model
(the Builder Pattern)
Use multiple simple objects to build a complex object step by step. This type of design pattern is the creation pattern, which provides the best way to create objects.
The essence of the Builder pattern is to decouple the assembly process (encapsulated with a director class for decoupling purposes) and create product-specific decoupling so that we don’t have to care about how each component is assembled.
Advantages:
1, the builder is independent and easy to expand. 2, easy to control the details of risk.Copy the code
Disadvantages:
1. Products must have common ground and limited scope. 2. If the internal changes are complex, there will be many construction classes.Copy the code
Key code:
2. Director: Controls the construction process, isolates the association between users and the construction process, and manages the dependence of the built instances.Copy the code
Usage Scenarios:
1. The object to be generated has a complex internal structure. 2. The internal properties of the object to be generated depend on each other.Copy the code
Implementation:
Step by step, the first is the head, then the body, left hand, right hand, left foot, right foot. First, the definition defines an abstract creation:
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();
}
Copy the code
In the drawing process, we make a thin person, and let the thin person inherit the abstract class
class PersonThinBuilder : PersonBuilder { public PersonThinBuilder(Graphics g,Pen p):base(g, P) {} public override void BuildHead() {g.rectangle (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); }}Copy the code
The commander class controls the construction process
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(); }}Copy the code
The client
private void button1_Click(object sender, EventArgs e)
{
Pen p = new Pen (Color.White );
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();
}
Copy the code
According to the results
This is a fat person and a thin person, and if you want to add other people, you add the corresponding class.