在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

相关推荐
一路向北North23 分钟前
Spring AI(6) :对话机器人-会话历史
java·人工智能·spring
RuoyiOffice1 小时前
超级个体接私活必看:后端+前端+移动端三端一体企业管理系统怎么选(2026)
java·spring boot·vue·uniapp·全栈·企业管理·接私活
java1234_小锋1 小时前
【免费】基于Spark实时电商用户行为分析与预测(Java版本+可视化大屏+Kafka+SpringBoot+Vue3) 锋哥原创出品,必属精品
java·spark·kafka·实时电商用户行为分析与预测系统
keyipatience2 小时前
日志和线程池
java·开发语言
鱼香鱼香rose2 小时前
java-2
java·开发语言·python
是小蟹呀^10 小时前
Spring Security + JWT 面试题整理
java·jwt·springsecurity
spencer_tseng12 小时前
Redis + Nacos.bat
java·windows·dos
troyzhxu13 小时前
列表查询的 GraphQL —— 一行代码终结你的 if-else 地狱!
java·springboot·graphql
孫治AllenSun14 小时前
【DataX】生产环境搭建DataX集群案例
java·开发语言·jvm