星期一, 4月 27, 2015

[DP] 建造者模式

根據大話設計模式書本內容,來練習狀態模式

擷取書本上的類別圖

[DP] 建造者模式-2


相關 class
namespace DP_Builder
{
    public abstract class PersonBuilder
    {
        public abstract void BuildHead();
        public abstract void BuildBody();
        public abstract void BuildArmLeft();
        public abstract void BuildArmRight();
        public abstract void BuildLegLeft();
        public abstract void BuildLegRight();
    }
}

using System.Drawing;

namespace DP_Builder
{
    public class PersonFatBuilder : PersonBuilder
    {
        private Graphics _g;
        private Pen _p;

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

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

        public override void BuildBody()
        {
            _g.DrawEllipse(_p, 40, 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);
        }
    }
}

using System.Drawing;

namespace DP_Builder
{
    public class PersonThinBuilder : PersonBuilder
    {
        private Graphics _g;
        private Pen _p;

        public PersonThinBuilder(Graphics g, Pen p)
        {
            this._g = g;
            this._p = 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);
        }
    }
}

namespace DP_Builder
{
    public 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();
        }
    }
}

在 Windows Form PictureBox 內進行測試
namespace DP_Builder
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            // 測試一
            Pen p = new Pen(Color.Red);
            PersonThinBuilder tb = new PersonThinBuilder(e.Graphics, p);
            PersonDirector pd = new PersonDirector(tb);
            pd.CreatePerson();

            // 測試二
            Pen p = new Pen(Color.Blue);
            PersonFatBuilder fb = new PersonFatBuilder(e.Graphics, p);
            PersonDirector pd = new PersonDirector(fb);
            pd.CreatePerson();
        }
    }
}
[DP] 建造者模式-1

沒有留言:

張貼留言