好菜没盐味不同 建造者模式

13.1 炒面没放盐

13.2 建造小人一

namespace 建造者模式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Pen p = new Pen(Color.Yellow);

            Graphics gThin = pictureBox1.CreateGraphics();

            gThin.DrawEllipse(p, 50, 20, 30, 30);
            gThin.DrawRectangle(p, 60, 50, 10, 50);
            gThin.DrawLine(p, 60, 50, 40, 100);
            gThin.DrawLine(p, 70, 50, 90, 100);
            gThin.DrawLine(p, 60, 100, 45, 150);
            gThin.DrawLine(p, 70, 100, 85, 150);



            Graphics gFat = pictureBox2.CreateGraphics();

            gFat.DrawEllipse(p, 50, 20, 30, 30);
            gFat.DrawEllipse(p, 45, 50, 40, 50);
            gFat.DrawLine(p, 50, 50, 30, 100);
            gFat.DrawLine(p, 80, 50, 100, 100);
            gFat.DrawLine(p, 60, 100, 45, 150);
            gFat.DrawLine(p, 70, 100, 85, 150);
        }

    }
}
View Code

13.3 建造小人二

namespace 建造者模式
{

    class PersonThinBuilder
    {
        private Graphics g;
        private Pen p;

        public PersonThinBuilder(Graphics g, Pen p)
        {
            this.g = g;
            this.p = p;
        }

        public void Build()
        {
            g.DrawEllipse(p, 50, 20, 30, 30);
            g.DrawRectangle(p, 60, 50, 10, 50);
            g.DrawLine(p, 60, 50, 40, 100);
            g.DrawLine(p, 70, 50, 90, 100);
            g.DrawLine(p, 60, 100, 45, 150);
            g.DrawLine(p, 70, 100, 85, 150);
        }
    }

    class PersonFatBuilder
    {
        private Graphics g;
        private Pen p;

        public PersonFatBuilder(Graphics g, Pen p)
        {
            this.g = g;
            this.p = p;
        }

        public void Build()
        {
            g.DrawEllipse(p, 50, 20, 30, 30);
            g.DrawEllipse(p, 45, 50, 40, 50);
            g.DrawLine(p, 50, 50, 30, 100);
            g.DrawLine(p, 80, 50, 100, 100);
            g.DrawLine(p, 60, 100, 45, 150);
            g.DrawLine(p, 70, 100, 85, 150);
        }
    }

}
View Code
namespace 建造者模式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Pen p = new Pen(Color.Yellow);

            Graphics gThin = pictureBox1.CreateGraphics();
            PersonThinBuilder ptb = new PersonThinBuilder(gThin, p);
            ptb.Build();

            Graphics gFat = pictureBox2.CreateGraphics();
            PersonFatBuilder pfb = new PersonFatBuilder(gFat, p);
            pfb.Build();
        }
    }

}
View Code

13.4 建造者模式

将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示,

可以将一个产品的内部表象与产品的生成过程分割开来,从而可以使一个建造过程生成具有不同的内部表象的产品对象,

如果使用建造者模式,用户只需指定需要建造的类型就可以得到它们,具体建造过程和细节无需知道,

namespace 建造者模式
{
    //抽象建造人的类,稳住建造过程,
    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();
        }
    }

}
View Code
namespace 建造者模式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            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();
        }

    }
}
View Code

13.5 建造者模式解析

何时使用建造者模式?

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

建造者模式的好处就是使得建造代码与表示代码分离,由于建造者隐藏了该产品是如何组装的,所以若需要改变一个产品的内部表示,只需要再定义一个具体的建造者就可以了,

13.6 建造者模式基本代码

namespace 建造者模式
{
    class Program
    {
        static void Main(string[] args)
        {
            Director director = new Director();

            Builder b1 = new ConcreteBuilder1();
            Builder b2 = new ConcreteBuilder2();

            director.Construct(b1);
            Product p1 = b1.GetResult();   //指挥者用ConcreteBuilder1的方法建造商品,
            p1.Show();

            director.Construct(b2);
            Product p2 = b2.GetResult();   //指挥者用ConcreteBuilder2的方法建造商品,
            p2.Show();

            Console.Read();
        }
    }

    //产品类,由多个部件组成,
    class Product
    {
        IList<string> parts = new List<string>();

        public void Add(string part)      //添加产品部件,
        {
            parts.Add(part);
        }

        public void Show()
        {
            Console.WriteLine("\n产品 创建 ----");   //列举所有产品部件,

            foreach (string part in parts)
            {
                Console.WriteLine(part);
            }
        }
    }

    //抽象建造者类,确定由两个部件组成PartA和PartB组成,
    //并声明一个得到产品建造后结果的方法GetResult,
    abstract class Builder
    {
        public abstract void BuildPartA();
        public abstract void BuildPartB();
        public abstract Product GetResult();
    }

    //具体建造者类,
    class ConcreteBuilder1 : Builder
    {
        private Product product = new Product();

        public override void BuildPartA()
        {
            product.Add("部件A");
        }

        public override void BuildPartB()
        {
            product.Add("部件B");
        }

        public override Product GetResult()
        {
            return product;
        }
    }

    //具体建造者类,
    class ConcreteBuilder2 : Builder
    {
        private Product product = new Product();

        public override void BuildPartA()
        {
            product.Add("部件X");
        }

        public override void BuildPartB()
        {
            product.Add("部件Y");
        }

        public override Product GetResult()
        {
            return product;
        }
    }

    //指挥者类,
    class Director
    {
        public void Construct(Builder builder)   //用来指挥建造过程,
        {
            builder.BuildPartA();
            builder.BuildPartB();
        }
    }

}
View Code

建造者模式是在当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时适用的模式,

posted @ 2019-08-01 13:48  _Huang95  阅读(421)  评论(0编辑  收藏  举报