建造者模式-C#改良實現

區分網上已有的通常建造者模式實現,我的以爲實現太單一了,本身google查了一些好的實現,挑了其中比較適合的,作個筆記。ide

# 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#的基本語法,並無額外在建立一個函數返回產品,相對更簡潔更"高大上",哈哈....函數

相關文章
相關標籤/搜索