建造者模式(Builder)

PersonBuilder.cs
using System.Drawing;

namespace WindowsFormsApp1
{
    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();
    }
}
PersonFatBuilder.cs
using System.Drawing;

namespace WindowsFormsApp1
{
    class PersonFatBuilder : PersonBuilder
    {
        public PersonFatBuilder(Graphics _g, Pen _p) : base(_g, _p) { }
        public override void BuildArmLeft()
        {
            _g.DrawLine(_p, 50, 50, 30, 100); // Arm Left
        }

        public override void BuildArmRight()
        {
            _g.DrawLine(_p, 80, 50, 100, 100); // Arm Right
        }

        public override void BuildBody()
        {
            _g.DrawEllipse(_p, 45, 50, 40, 50); // Body
        }

        public override void BuildHead()
        {
            _g.DrawEllipse(_p, 50, 20, 30, 30); // Head
        }

        public override void BuildLegLeft()
        {
            _g.DrawLine(_p, 60, 100, 45, 150); // Leg Left
        }

        public override void BuildLegRight()
        {
            _g.DrawLine(_p, 70, 100, 85, 150); // Leg Right
        }
    }
}
PersonThinBuilder.cs
using System.Drawing;

namespace WindowsFormsApp1
{
    class PersonThinBuilder : PersonBuilder
    {
        public PersonThinBuilder(Graphics _g, Pen _p) : base(_g, _p) { }
        public override void BuildArmLeft()
        {
            _g.DrawLine(_p, 60, 50, 40, 100); // Left hand
        }

        public override void BuildArmRight()
        {
            _g.DrawLine(_p, 70, 50, 90, 100); // Right hand
        }

        public override void BuildBody()
        {
            _g.DrawRectangle(_p, 60, 50, 10, 50); // Body
        }

        public override void BuildHead()
        {
            _g.DrawEllipse(_p, 50, 20, 30, 30); // Head
        }

        public override void BuildLegLeft()
        {
            _g.DrawLine(_p, 60, 100, 45, 150); // Left Leg
        }

        public override void BuildLegRight()
        {
            _g.DrawLine(_p, 70, 100, 85, 150); // Leg Right
        }
    }
}
PersonDirector.cs
namespace WindowsFormsApp1
{
    class PersonDirector
    {
        private PersonBuilder _builder;
        public PersonDirector(PersonBuilder _builder)
        {
            this._builder = _builder;
        }

        public void CreatePerson()
        {
            _builder.BuildHead();
            _builder.BuildBody();
            _builder.BuildArmLeft();
            _builder.BuildArmRight();
            _builder.BuildLegLeft();
            _builder.BuildLegRight();
        }
    }
}
MainForm.cs
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            pictureBox1.Paint += new PaintEventHandler(delegate {
                Pen p = new Pen(Color.Red,5);
                Graphics g = pictureBox1.CreateGraphics();
                PersonDirector director = new PersonDirector(new PersonThinBuilder(g, p));
                director.CreatePerson();
            });

            pictureBox2.Paint += new PaintEventHandler(delegate
            {
                Pen p = new Pen(Color.Black,5);
                Graphics g = pictureBox2.CreateGraphics();
                PersonDirector director = new PersonDirector(new PersonFatBuilder(g, p));
                director.CreatePerson();
            });
        }
    }
}

你可能感兴趣的:(建造者模式(Builder))