设计模式之-建造者模式

定义:如果将一个复杂对象的构建和它的表示分离,使得同样的构建过程可以创建不同的表示。

 

 

什么时候使用建造者?

主要用于创建一些复杂的对象,这些对象内部构建间的建造顺序通常是稳定的,但对象内部的构建通常面临着复杂的变化。

 

将一个复杂对象的构建与它的表示分开,使得同样的构建过程可以创建不同的表示。如果我们用了建造者模式,那么用户就只需要指定需要建造的类型就可以得到它们,而具体建造的过程和细节就不需要知道了。(过程是稳定的,但是具体的细节是不同的)

建造者模式中一个很重要的类,指挥者(Director),用它来控制建造过程,也用它来隔离用户和建造过程的关联。
实例应用,画一个小人

 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();
    }
View Code
    class PersonThinBuilder:PersonBuilder
    {
        //具体的细节
        public PersonThinBuilder(Graphics g,Pen p) : base(g, p)//base用于在派生类中访问重写的基类成员,Base 是子类中引用父类
        {

        }
        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);
        }
    }
View Code
     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();
        }
    }
View Code

 客户端代码如下

Pen p=new Pen(Color.Yellow);
PersonThinBuilder ptb = new PersonThinBuilder (pictureBox1.CreateGraphics(),p);
PersonDirector pdThin=new PersonDirector (ptb );
pdThin.CreatePerson();

PersonFatBuilder ptb2 = new PersonFatBuilder (pictureBox2.CreateGraphics(),p);
PersonDirector pdFat=new PersonDirector (ptb2);
pdThin.CreatePerson();

 C++版本

#include<iostream>
using namespace std;
class Builder
{
public:
    virtual void BuildHead(){}
    virtual void BuildBody(){}
    virtual void BuildLeftArm(){}
    virtual void BuildRightArm(){}
    virtual void BuildLeftLeg() {}
    virtual void BuildRightLeg(){}
};
class ThinBuilder :public Builder
{
public:
    void BuildHead()
    {
        cout << "Build thin head" << endl;
    }
    void BuildBody()
    {
        cout << "Build thin body" << endl;
    }
    void BuildLeftArm()
    {
        cout << "Build thin leftarm" << endl;
    }
    void BuildRightArm()
    {
        cout << "Build thin rightarm" << endl;
    }
    void BuildLeftLeg()
    {
        cout << "Build thin leftleg" << endl;
    }
    void BuildRightLeg()
    {
        cout << "Build thin rightleg" << endl;
    }
};
//指挥者
class Director
{
private:
    Builder* MyBuilder;
public:
    Director(Builder* builder)
    {
        MyBuilder = builder;
    }
    void Create()
    {
        MyBuilder->BuildHead();
        MyBuilder->BuildBody();
        MyBuilder->BuildLeftArm();        
        MyBuilder->BuildRightArm();
        MyBuilder->BuildLeftLeg();
        MyBuilder->BuildRightLeg();
    }
};
int main()
{
    ThinBuilder thin;
    Director director(&thin);
    director.Create();
    return 0;
}
View Code

 

posted @ 2021-09-02 23:08  HelloWorld庄先生  阅读(38)  评论(0编辑  收藏  举报