• 建造型模式记录


    建造者模式

    建造者模式是将创建和表示进行分离,使得创建的过程中能够创建不同的表示。
    据说建造者-指挥者模式已经不是常用了,这里只是学习思想
    只要实现了创建和表示分离就好了,流式的配备一台电脑
    将电脑的展示和电脑的组成分开

      class Computer
        {
            private Computer(Builder builder)
            {
                HandDisk = builder.handDisk;
                Memroy = builder.memroy;
                MainBoard = builder.mainBoard;
                VideoCard = builder.videoCard;
                PowerSupply = builder.prowerSupply;
                CUP = builder.cUP;
            }
            public string HandDisk { get;  }
            public string Memroy { get;  }
            public string MainBoard { get; }
            public string VideoCard { get;  }
            public string PowerSupply { get; }
            public string CUP { get; }
    
            public class Builder
            {
                string HandDisk = "西部数据";
                string Memroy = "金士顿";
                string MainBoard = "华硕";
                string VideoCard = "铭影";
                string ProwerSupply = "冷静王";
                string CUP = "AMD";
    
                public string handDisk { get=> HandDisk; set => HandDisk = value; }
                public string memroy { get => Memroy; set => Memroy = value; }
                public string mainBoard { get => MainBoard; set => MainBoard = value; }
                public string videoCard { get => VideoCard; set => VideoCard = value; }
                public string prowerSupply { get => ProwerSupply; set => ProwerSupply = value; }
                public string cUP { get => CUP; set => CUP = value; }
    
                public Computer computer() 
                {
                    return new Computer(this);
                }
            }
    

    电脑分成硬盘、内存、主板、显卡、电源和cup,他们展示在computer上(只有get,拼装使用构造函数里面是内部类),在这个里面有个内部类是电脑的拼装,
    里面是电脑的set/get方法,并且含有默认值。这样将展示和组成分开了。
    使用:

       class User
        {
            public void BuyComputer() 
            {
                //默认配置电脑
                Computer computer = new Computer.Builder().computer();
                Console.WriteLine("cup:"+computer.CPU+"...");
                //自定义配置
                Computer.Builder build = new Computer.Builder();
                build.cPU = "intel";
                Computer computer2 = build.computer();
                Console.WriteLine("cup:" + computer2.CPU + "...");
            }
    

    这里的user的自定义配置有些奇怪,意思表达到就好了。。这里应该还能改进的更好

  • 相关阅读:
    正则中[A-z]与[A-Za-z]的区别
    .Net Core 缓存方式(二)DistributedSqlServerCache实现(2)
    .Net Core 缓存方式(二)分布式缓存及MemoryDistributedCache 实现(1)
    anaconda安装后spyder打不开的解决方法
    Pandas
    CrawlSpider、分布式、增量式
    Scrapy之数据解析与数据持久化存储
    封装axios库
    vue全国省市选择vue组件
    html+jq实现全国省的单选,弹框输入input
  • 原文地址:https://www.cnblogs.com/liuyang95/p/13371056.html
Copyright © 2020-2023  润新知