在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

相关推荐
Derek_Smart5 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP5 小时前
MyBatis-mybatis入门与增删改查
java
孟陬9 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌9 小时前
一站式了解四种限流算法
java·后端·go
华仔啊9 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝10 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java
Nyarlathotep011311 小时前
SpringBoot Starter的用法以及原理
java·spring boot
wuwen511 小时前
WebFlux + Lettuce Reactive 中 SkyWalking 链路上下文丢失的修复实践
java
SimonKing11 小时前
GitHub 10万星的OpenCode,正在悄悄改变我们的工作流
java·后端·程序员