建造者模式+C#窗口实现

目录

一、建造者模式

二、建造者模式角色

三、代码实例一

四、代码实例二

五、代码实例三

、UML图


一、建造者模式

将一个复杂对象的构建与其表示分离,使得同样构建过程可以创建不同表示。就像下面的建造小人例子,同样的构建过程就创建了不同的小人。

建造者模式使得产品内部可以独立变化,客户不必知道其实现细节。

各个建造者可以独立变化

最终的产品易于控制。

本质是分离整体构建算法和部件构造

1、有些情况下,一个对象会有一些重要的性质(体会下建造过程),在它们没有恰当的值之前,对象不能作为一个完整的产品使用

2、一类事物必须按顺序赋值

3、建造者返回给客户端的是建造好的产品对象。

二、建造者模式角色

建造者抽象类:就像下面的PersonBuilder抽象类,它提供了各个函数的操作。

具体建造者:就像下面的ThinnerBuilder类,实现抽象类方法,完成建造过程。

指导者:用来实现建造者抽象类,使得可以统一实现建造过程。由指导者指导装配过程,含有建造者对象的引用。

产品:一个最后的结果

三、代码实例一

第一个抽象类:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建造者模式1
{
    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 BuildArmLeft();
        public abstract void BuildArmRight();
        public abstract void BuildBody();
        public abstract void BuildLegLeft();
        public abstract void BuildLegRight();
    }
}

下面的具体建造者:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建造者模式1
{
    class ThinnerBuilder : PersonBuilder
    {


        public ThinnerBuilder(Graphics g,Pen p) : base(g, p) { }
        
        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 BuildBody()
        {
            g.DrawRectangle(p, 60, 50, 10, 50);

        }
        public override void BuildHead()
        {
            g.DrawEllipse(p, 50, 20, 30, 30);

        }
        public override void BuildLegLeft()
        {
            g.DrawLine(p, 60, 100, 45, 150);

        }

        public override void BuildLegRight()
        {
            g.DrawLine(p, 70, 100, 85, 150);
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建造者模式1
{
    class FatterBuilder:PersonBuilder
    {
        public FatterBuilder(Graphics g, Pen p) : base(g, p) { }

        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 BuildBody()
        {
            g.DrawRectangle(p, 90, 70, 40, 70);

        }
        public override void BuildHead()
        {
            g.DrawEllipse(p, 50, 20, 30, 30);

        }
        public override void BuildLegLeft()
        {
            g.DrawLine(p, 60, 100, 45, 150);

        }

        public override void BuildLegRight()
        {
            g.DrawLine(p, 70, 100, 85, 150);
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 建造者模式1
{
    class PersonManage
    {
        private PersonBuilder per;

        public PersonManage(PersonBuilder per)
        {
            this.per = per;
        }
        public void CreatePerson()
        {
            per.BuildHead();
            per.BuildArmLeft();
            per.BuildArmRight();
            per.BuildBody();
            per.BuildLegLeft();
            per.BuildLegRight();
        }
    }
}

Form窗体程序:

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

namespace 建造者模式1
{
    public partial class Form1 : Form
    {
        public Form1()
        {

            // Graphics pictureBox= CreateGraphics();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Pen p = new Pen(Color.Black);
            ThinnerBuilder pb = new ThinnerBuilder(pictureBox2.CreateGraphics(), p);
            PersonManage pp = new PersonManage(pb);
            pp.CreatePerson();

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Pen p = new Pen(Color.Black);
            FatterBuilder pb = new FatterBuilder(pictureBox3.CreateGraphics(), p);
            PersonManage pp = new PersonManage(pb);
            pp.CreatePerson();
        }
    }
}

实现结果:

这个胖小孩画的实在是找不到方向..................

四、代码实例二

 产品角色:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    abstract class Computer//需要建造的抽象类
    {
        private string type;
        private string cpu;
        private string ram;
        private string monitor;

        public string Type { get => type; set => type = value; }
        public string Cpu { get => cpu; set => cpu = value; }
        public string Ram { get => ram; set => ram = value; }
        public string Monitor { get => monitor; set => monitor = value; }
        public abstract string ToString();
    }
}

具体产品类,为两个型号的电脑:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    class ComputerOne : Computer//第一个类型
    {
        private string graphicCard;//这个电脑类型有独立显卡

        public ComputerOne()
        {
            this.Type = "LianXiang1";
        }

        public string GraphicCard { get => graphicCard; set => graphicCard = value; }

        public override string ToString()
        {
            return "型号:" + this.Type + "\nCPU:" + this.Cpu + "\n内存:" + this.Ram + "\n显示器:" + this.Monitor+"\n显卡:"+this.GraphicCard;
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    class ComputerTwo : Computer
    {
        public ComputerTwo()
        {
            this.Type = "HuaShuo1";
        }
        public override string ToString()
        {
            return "型号:" + this.Type + "\nCPU:" + this.Cpu + "\n内存:" + this.Ram + "\n显示器:" + this.Monitor;
        }

    }
}

 建造者角色:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    interface ComputerBuilder
    {
        void BuildCpu();//建造过程
        void BuildRam();
        void BuildMonitor();
        Computer GetResult();
    }
}

 具体建造者:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    class ComputerOneBuilder : ComputerBuilder
    {
        private ComputerOne c1 = new ComputerOne();
        public void BuildCpu()
        {
            c1.Cpu = "i1";
        }

        public void BuildMonitor()
        {
            c1.Ram = "14 英寸 1280*800";
        }

        public void BuildRam()
        {
            c1.Monitor = "4GB 1333MHZ";
        }

        public Computer GetResult()
        {
            return c1;
        }//返回对象
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BuilderPattern
{
    class ComputerTwoBuilder : ComputerBuilder
    {
        private ComputerTwo c2 = new ComputerTwo();
        public void BuildCpu()
        {
            c2.Cpu = "i2";
        }

        public void BuildMonitor()
        {
            c2.Monitor = "13 英寸 1280*900";
        }

        public void BuildRam()
        {
            c2.Ram = "4 GB 1666MHZ";
        }

        public Computer GetResult()
        {
            return c2;
        }
    }
}

 测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BuilderPattern
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            ComputerDirector man = new ComputerDirector();
            Computer one = man.ConstructOne();
            Console.WriteLine(one.ToString());
            one = man.ConstructTwo();
            Console.WriteLine(one.ToString());
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

五、代码实例三

 

、UML图

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值