5、面向对象语言23种设计模式-建造者模式

一、什么是建造者模式

建造者模式关注的是对复杂对象的创建。

它有四个角色,按我的理解的话分别是:

比如现在需要组装一台电脑

  • 用户:最终使用电脑的角色
  • 装机员:组装电脑的角色
  • 说明书:电脑的零件清单等
  • 厂家销售:获取各个零件

可能不太准确,大概就这么个意思。

二、建造者模式的使用场景

这个在平时项目中用的不是很多,因为只有当流程固定的时候才能使用建造者模式,其他时候就不太适用了。

当我们最终需要创建的对象依赖于其他对象的创建时可以使用建造者模式,将创建对象的过程转移,这样上端就跟之前一样能直接获得最终需要使用的对象。

三、如何实现建造者模式

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5ZWK6ISR6KKLX1lB,size_20,color_FFFFFF,t_70,g_se,x_16

用户Program.cs:

namespace Builder
{
    class Program
    {
        static void Main(string[] args)
        {
            {
                AbstractBuilder builder = new SaleHW();
                Installer installer = new Installer(builder);
                Computer computer = installer.GetComputer();
                Console.WriteLine("我的计算机:"+ computer.Name);
            }
            Console.WriteLine("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");

            {
                AbstractBuilder builder = new SaleXM();
                Installer installer = new Installer(builder);
                Computer computer = installer.GetComputer();
                Console.WriteLine("我的计算机:" + computer.Name);
            }
        }
    }
}

厂家销售SaleHW.cs:

namespace Builder
{
    public class SaleHW : AbstractBuilder
    {
        private Host _Host = null;
        private Keyboard _Keyboard = null;
        private Monitor _Monitor = null;
        private Mouse _Mouse = null;
        private VoiceBox _VoiceBox = null;

        public override void Host()
        {
            this._Host = new Host()
            {
                Name = "华为主机"
            };
        }

        public override void Keyboard()
        {
            this._Keyboard = new Keyboard()
            {
                Name = "华为键盘"
            };
        }

        public override void Monitor()
        {
            this._Monitor = new Monitor()
            {
                Name = "华为显示器"
            };
        }

        public override void Mouse()
        {
            this._Mouse = new Mouse()
            {
                Name = "华为鼠标"
            };
        }

        public override void VoiceBox()
        {
            this._VoiceBox = new VoiceBox()
            {
                Name = "华为音箱"
            };
        }
        public override Computer Computer()
        {
            Console.WriteLine("组装 {0} {1} {2} {3} {4}", this._Host.Name, this._Keyboard.Name, this._Monitor.Name, this._Mouse.Name, this._VoiceBox.Name);

            return new Computer(_Host, _Keyboard, _Monitor, _Mouse, _VoiceBox)
            {
                Name = "华为计算机"
            };
        }
    }
}

装机员Installer.cs:

namespace Builder
{
    public class Installer
    {
        private AbstractBuilder _AbstractBuilder = null;
        public Installer(AbstractBuilder builder)
        {
            this._AbstractBuilder = builder;
        }

        public Computer GetComputer()
        {
            this._AbstractBuilder.Host();
            this._AbstractBuilder.Keyboard();
            this._AbstractBuilder.Monitor();
            this._AbstractBuilder.Mouse();
            this._AbstractBuilder.VoiceBox();

            return this._AbstractBuilder.Computer();
        }
    }
}

说明书AbstractBuilder.cs:

namespace Builder
{
    public abstract class AbstractBuilder
    {
        public abstract void Host();

        public abstract void Keyboard();

        public abstract void Monitor();

        public abstract void Mouse();

        public abstract void VoiceBox();

        public abstract Computer Computer();
    }
}

各个零件:

//Computer.cs
namespace Builder.Models
{
    public class Computer
    {
        public Computer(Host host,Keyboard keyboard,Monitor monitor,Mouse mouse,VoiceBox voiceBox)
        {

        }

        public string Name { get; set; }
    }
}

//Host.cs
namespace Builder.Models
{
    public class Host
    {
        public string Name { get; set; }
    }
}

//Keyboard.cs
namespace Builder.Models
{
    public class Keyboard
    {
        public string Name { get; set; }
    }
}

//Monitor.cs
namespace Builder.Models
{
    public class Monitor
    {
        public string Name { get; set; }
    }
}

//Mouse.cs
namespace Builder.Models
{
    public class Mouse
    {
        public string Name { get; set; }
    }
}


//VoiceBox.cs
namespace Builder.Models
{
    public class VoiceBox
    {
        public string Name { get; set; }
    }
}

四、建造者模式的优缺点

只有当流程固定的时候才能使用建造者模式,其他时候就不太适用了。

将创建对象的细节转移,让上端不在关注创建的过程,方便上端的使用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊脑袋_YA

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值