在C# 中使用建造者模式

C# 的建造者模式比java强大的多, 它提供了 return this的语法, 可以让建造者模式使用链式语法. Action关键字可以使配置更加灵活

举例 如下:

csharp 复制代码
    public class ComputerBuilderFactory
    {
        public static IComputerBuilder CreateBuilder()
        {
            return new ComputerBuilder();
        }
    }
csharp 复制代码
    public interface IComputerBuilder
    {
        Computer Build();
        IComputerBuilder SetHost(string hostName);
        IComputerBuilder SetIp(string ip);
        IComputerBuilder SetHardWare(Action<HardWareWrapper> config);
    }
csharp 复制代码
    public class HardWareWrapper
    {
        private readonly HardWare hardWare;

        public HardWareWrapper(HardWare hardWare)
        {
            this.hardWare = hardWare;
        }


        public HardWareWrapper SetCpu(string cpu)
        {
            hardWare.Cpu = cpu;
            return this;
        }

        public HardWareWrapper SetMemory(string memory)
        {
            hardWare.Memory = memory;
            return this;
        }

        public HardWareWrapper SetStorage(string storage)
        {
            hardWare.Storage = storage;
            return this;
        }

    }
csharp 复制代码
    public class HardWare
    {
        public string Cpu { get; set; } = string.Empty;
        public string Memory { get; set; } = string.Empty;
        public string Storage { get; set; } = string.Empty;
    }
csharp 复制代码
    public class ComputerBuilder : IComputerBuilder
    {

        private HardWare hardWare = new();
        private string hostName = string.Empty;
        private string ip = string.Empty;
        public Computer Build()
        {
            return new(hostName, ip, hardWare);
        }

        public IComputerBuilder SetHardWare(Action<HardWareWrapper> config)
        {
            HardWareWrapper wrapper = new HardWareWrapper(hardWare);
            config?.Invoke(wrapper);
            return this;
        }

        public IComputerBuilder SetHost(string hostName)
        {
            this.hostName = hostName ?? string.Empty;
            return this;
        }

        public IComputerBuilder SetIp(string ip)
        {
            this.ip = ip ?? string.Empty;
            return this;
        }
    }
csharp 复制代码
    public class Computer
    {
        public Computer(string ip, string host, HardWare myHardWare)
        {
            Ip = ip;
            Host = host;
            MyHardWare = myHardWare;
        }

        public string Ip { get; }
        public string Host { get; }

        public HardWare MyHardWare { get; }

        public override string ToString()
        {
            return $"ip:{Ip}, host:{Host}, Memory:{MyHardWare.Memory}, Storage:{MyHardWare.Storage}, Cpu:{MyHardWare.Cpu}";
        }
    }

可以在builder 中 写set方法, 返回return

在computer 实体类中通过构造函数传入

HardWare的属性配置可以用Action 传入HardWare 类型的参数进行配置, HardWare和其他字段一起以private字段的形式留在ComputerBuilder 中, 使用HardWareWrapper 配置 HardWare, 构造器传入HardWareWrapper 的 HardWare

相关推荐
考虑考虑4 小时前
JDK27结构化并发
java·后端·java ee
Cosolar6 小时前
监控指标全正常,订单接口从 80ms 涨到 4s 故障复盘
java·后端·面试
会编程的吕洞宾15 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤15 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha15 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
RobinXYuan16 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!16 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
Risehuxyc16 小时前
HttpClient怎么传POST参数
c#
SimonKing17 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员
CoderYanger17 小时前
Java EE 进阶:11. SpringBoot 统一功能处理(第二弹):DispatcherServlet 源码分析+适配器模式
java·数据库·hive·spring boot·面试·java-ee·mybatis