建造者模式-C#改良实现

区分网上已有的一般建造者模式实现,个人觉得实现太单一了,自己google查了一些好的实现,挑了其中比较适合的,做个笔记。

    # region 标准Builder模式实现

    // 产品
    class Television
    {
        public string Code { get; set; }
        public string DisplayTechnology { get; set; }
        public string HDFormat { get; set; }
        public string ScreenType { get; set; }
        public string Size { get; set; }
        public string Feature { get; set; }
    }

    // 抽象Builer
    abstract class TelevisionBuilder{
        protected Television television;

        public abstract void SetCode();//ProductCode
        public abstract void SetDisplayTechnology(); //LCD, LED, OLED, Plasma, CRT
        public abstract void SetHDFormat(); // Full HD, HD Ready, Ultra HD, None
        public abstract void SetScreenType(); //Curved, Flat, Standard
        public abstract void SetSize(); // 22, 32, 40, 42, 54
        public abstract void SetFeature(); //3D, Smart, Standard , HDMI Ports, USB Ports, Built in WIFI, Ethernet, Remote
 
        //获取产品
        public Television Television { get => television; }
    }

    // 具体Builder(具体实现1)
    class FullHD40TVBuilder : TelevisionBuilder
    {
        public FullHD40TVBuilder() => television = new Television();

        public override void SetCode() => television.Code = "FullHD40TV";

        public override void SetDisplayTechnology() => television.DisplayTechnology = "LCD";

        public override void SetHDFormat() => television.HDFormat = "FullHD";

        public override void SetScreenType() => television.ScreenType = "Flat";

        public override void SetSize() => television.Size = "40";

        public override void SetFeature() => television.Feature = "1 HDMI Ports, 1 USB Ports, Remote";
    }

    // 具体Builder(具体实现2)
    class SMARTLED54TVBuilder : TelevisionBuilder
    {
        public SMARTLED54TVBuilder() => television = new Television();

        public override void SetCode() => television.Code = "SMARTLED54TV";

        public override void SetDisplayTechnology() => television.DisplayTechnology = "LED";

        public override void SetHDFormat() => television.HDFormat = "FullHD";

        public override void SetScreenType() => television.ScreenType = "Flat";

        public override void SetSize() => television.Size = "54";

        public override void SetFeature() => television.Feature = "2 HDMI Ports, 2 USB Ports, Built in WIFI, Ethernet, Remote";
    }
    
    // 环境角色(解耦作用)
    class TelevisionContext {
        public void Construction(TelevisionBuilder builder)
        {
            builder.SetCode();
            builder.SetDisplayTechnology();
            builder.SetFeature();
            builder.SetHDFormat();
            builder.SetScreenType();
            builder.SetSize();
        }

    }

    #endregion
    
    // 在客户端调用:
    var builder1 = new SMARTLED54TVBuilder();
    var builder2 = new FullHD40TVBuilder();
    var ctx = new TelevisionContext();
    ctx.Construction(builder1);
    ctx.Construction(builder2);

    var product1 = builder1.Television;
    var product2 = builder2.Television;
    Console.WriteLine($"产品1生产完成,详细:{JsonConvert.SerializeObject(product1)}");
    Console.WriteLine($"产品2生产完成,详细:{JsonConvert.SerializeObject(product2)}");
    

相比较来讲,这个实现更细致,而且充分利用了C#的基本语法,并没有额外在创建一个函数返回产品,相对更简洁更"高大上",哈哈....

转载于:https://www.cnblogs.com/zhiyong-ITNote/p/10869240.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值