大话设计-建造者模式

建造代码和表示代码分离,建造顺序通常是稳定的,建造者隐藏了产品的组装过程,如果要增加产品的组装,可以新增具体的建造者。

#define PPP

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Pen p = new Pen(Color.Yellow);
            Graphics g=this.CreateGraphics();
            PersonThinBuilder personThinBuilder = new PersonThinBuilder(g, p);
            PersonFatBuilder personFatBuilder = new PersonFatBuilder(g, p);

            PersonDirector personDirector = new PersonDirector(personThinBuilder);
            personDirector.CreatePerson();
            personDirector = new PersonDirector(personFatBuilder);
            personDirector.CreatePerson();
        }        
    }


    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()
        {
            throw new NotImplementedException();
        }

        public override void BuildArmRight()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegRight()
        {
            throw new NotImplementedException();
        }
    }

    class PersonFatBuilder : PersonBuilder
    {
        public PersonFatBuilder(Graphics g, Pen p) : base(g, p)
        {
        }

        public override void BuildArmLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildArmRight()
        {
            throw new NotImplementedException();
        }

        public override void BuildBody()
        {
            throw new NotImplementedException();
        }

        public override void BuildHead()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegRight()
        {
            throw new NotImplementedException();
        }
    }

    class PersonDirector
    {
        private PersonBuilder pb;
        public PersonDirector(PersonBuilder person)
        {
            this.pb = person;
        }

        public void CreatePerson()
        {
            pb.BuildHead();
            pb.BuildBody();
            pb.BuildArmLeft();
            pb.BuildArmRight();
            pb.BuildLegLeft();
            pb.BuildLegRight();
        }
    }
}
#define PPP

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Pen p = new Pen(Color.Yellow);
            Graphics g=this.CreateGraphics();
            PersonThinBuilder personThinBuilder = new PersonThinBuilder(g, p);
            PersonFatBuilder personFatBuilder = new PersonFatBuilder(g, p);

            PersonDirector personDirector = new PersonDirector(personThinBuilder);
            personDirector.CreatePerson();
            personDirector = new PersonDirector(personFatBuilder);
            personDirector.CreatePerson();
        }        
    }


    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()
        {
            throw new NotImplementedException();
        }

        public override void BuildArmRight()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegRight()
        {
            throw new NotImplementedException();
        }
    }

    class PersonFatBuilder : PersonBuilder
    {
        public PersonFatBuilder(Graphics g, Pen p) : base(g, p)
        {
        }

        public override void BuildArmLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildArmRight()
        {
            throw new NotImplementedException();
        }

        public override void BuildBody()
        {
            throw new NotImplementedException();
        }

        public override void BuildHead()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegLeft()
        {
            throw new NotImplementedException();
        }

        public override void BuildLegRight()
        {
            throw new NotImplementedException();
        }
    }

    class PersonDirector
    {
        private PersonBuilder pb;
        public PersonDirector(PersonBuilder person)
        {
            this.pb = person;
        }

        public void CreatePerson()
        {
            pb.BuildHead();
            pb.BuildBody();
            pb.BuildArmLeft();
            pb.BuildArmRight();
            pb.BuildLegLeft();
            pb.BuildLegRight();
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/bibi-feiniaoyuan/p/builderPattern.html
今日推荐