在构建器模式中管理订单的首选方法是什么?

2023-11-25

我创建了一个流畅的构建器样式模式来帮助加载测试数据。某些方法的顺序很重要,并且想知道管理正确顺序的首选方法是什么。

我目前有以下情况:

using NUnit.Framework;

[TestFixture]
public class DataBuilderTests
{
    [Test]
    public void Can_NAME()
    {
        new DataLoader()
            .Start() // must be called first
            .Setup() // then called next
            .LoadEmployees() // optional order not NB
            .LoadProducts() // optional order not NB
            .StartCleanup() // begin cleanup
            .CleanupEmployees() // optional order not NB
            .CleanupProducts() // optional order not NB
            .End();
    }
}

public class DataLoader
{
    public DataBuilderSetup Start()
    {
        return new DataBuilderSetup(this);       
    }
}

public class DataBuilderSetup
{
    private readonly DataLoader _dataLoader;

    public DataBuilderSetup(DataLoader dataLoader)
    {
        _dataLoader = dataLoader;
    }

    public DataBuilderOptions Setup()
    {
        // do setup
        return new DataBuilderOptions(_dataLoader);
    }
}

public class DataBuilderOptions
{
    private readonly DataLoader _dataLoader;

    public DataBuilderOptions(DataLoader dataLoader)
    {
        _dataLoader = dataLoader;
    }

    public DataBuilderOptions LoadEmployees()
    {
        // load
        return this;
    }

    public DataBuilderOptions LoadProducts()
    {
        // load
        return this;
    }

    public DataBuilderCleanupOptions StartCleanup()
    {
        return new DataBuilderCleanupOptions(_dataLoader);
    }
}

public class DataBuilderCleanupOptions
{
    private readonly DataLoader _dataLoader;

    public DataBuilderCleanupOptions(DataLoader dataLoader)
    {
        _dataLoader = dataLoader;
    }

    public DataBuilderCleanupOptions CleanupEmployees()
    {
        // cleanup
        return this;
    }

    public DataBuilderCleanupOptions CleanupProducts()
    {
        // cleanup
        return this;
    }

    public DataLoader End()
    {
        return _dataLoader;
    }
}

在 Java 中(C# 及其多重继承不应使其有任何不同),您可以这样做:

声明一组仅包含一个方法的接口:

Interface DoFirstThing { // could be renamed to "BuilderOnStart" or "BuilderStartingState"
    DoSecondThing doFirst();
}

Interface DoSecondThing {
    DoLastThing doSecond();
}

Interface DoLastThing {
    BuilderReady doLast();
}

Interface BuilderReady {
    Result build();
}

class BuilderWithForcedSequence implements DoFirstThing, DoSecondThing, DoLastThing, BuilderReady {

     // implement all

}

最后,您需要一些工厂或工厂方法来设置该构建器的初始状态:

public DoFirstThing createNewBuilderWithForcedSequence(requiredParameters){
    return new BuilderWithForcedSequence(requiredParameters);
}

这将产生 Builder,强制排序构建方法(它们应该从doThat到有意义的东西),只能调用doFirst(), 在那之后doSecond()...等等,直到最终状态,当对象将被构建时,使用build() method.

Result result = builder.doFirst().doSecond().doLast().build();


EDIT:
哎呀,这正是你的方法:)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在构建器模式中管理订单的首选方法是什么? 的相关文章

随机推荐

  • 生成迷宫的好算法是什么? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 目前不接受答案 假设你想要一个 N M 网格上的简单迷宫 有一条路径通过 并且有很多死胡同 但这看起来 正确 即就像有人手工制作的 没有太多微小的死胡同和所有这些
  • org.apache.spark.rpc.RpcTimeoutException:Futures 在 [120 秒] 后超时。这个超时由spark.rpc.lookupTimeout控制

    将 Spark 应用程序提交到 YARN 时 出现与容器相关的以下错误 HADOOP 2 7 3 SPARK 2 1 环境在单节点集群中运行伪分布式模式 该应用程序在本地模型中运行时可以完美运行 但是尝试使用 YARN 作为 RM 在集群模
  • 在Android中以编程方式创建基于VpnService的L2TP/IPSec VPN

    我搜索了一天 关于基于VpnService创建L2TP IPSec VPN 但没有匹配到的结果 使用本地VPN https github com hexene LocalVPN 我可以基于 VPNService 创建一个 VPN OpenV
  • Solr - 如何“分组”和“限制”?

    假设我从数据库中索引了以下内容 Id Code Description 1 A1 Hello world 2 A1 Hello world 123 3 A1 World hello hi 4 B1 Quick fox jumped 5 B1
  • 在Vue中将多个事件绑定到v-on指令

    在 jQuery 中 您可以通过执行以下操作来绑定多个事件 myDiv on touchstart mousedown more code here 据我了解 这会监听touchstart OR mousedown同时地 但我不知道如何用
  • 将单词转换为字符列表[重复]

    这个问题在这里已经有答案了 我可以将一个句子分成单独的单词 如下所示 string This is a string with words string split This is a string with words 但我不知道如何将单
  • 从 formatCurrency 中删除货币符号

    我正在尝试格式化从数据库获取的收入总额 并使用 php 的 NumberFormatter 类和 formatCurrency 方法 但是 我不想用这个打印出实际的欧元 欧元符号 我只想要简单的数字 带有逗号和小数点 例子 1234 56
  • 手电筒相机2 API

    Android Camera2 API 中可以同时使用相机预览和手电筒吗 当我尝试使用时CameraManager setTorchMode String cameraId boolean enabled 当相机未打开时它工作正常 但是当相
  • 将 Javascript 回调传递给 Qml 中的 C++ 调用方法

    在 C 中 我有一个带有可调用函数的类 我想做的是从 QML Javascript 调用该方法 我已经开始工作 并向其传递一个 Javascript 回调 在代码中 我定义我的类如下 class MyObject public QObjec
  • Rails - AJAX 模态对话框?

    我有兴趣学习如何 AJAX 模式对话框 通常 如果我想向网站添加模式对话框 我会在主 JS 文件中添加 jquery UI 对话框代码 并将其绑定到 ID 我相信使用 Rails 我可以创建一个链接 它从服务器获取所有对话框代码 然后打开对
  • 将 TBytes (UTF-16) 转换为字符串的最佳方法是什么?

    在 Delphi 2009 中将声明为 TBytes 的字节数组转换为 unicode 字符串的最佳方法是什么 在我的特定情况下 TBytes 数组已经具有 UTF 16 编码数据 每个字符 2 个字节 由于 TBytes 不存储空终止符
  • 为什么 vscode 从打字稿缓存导入包

    我已经开始使用 cli 生成一个反应本机项目 该应用程序似乎工作得很好 但我注意到导入指向打字稿缓存而不是本地节点模块 我什至没有使用打字稿 IDE vscode 1 19 3 import React Component from rea
  • SwiftUI - 如何检测长按按钮?

    我有一个按钮 当按下它时 它会执行一些操作 但我想修改同一个按钮来检测较长的按下时间 并执行一组不同的过程 如何修改此代码以检测长按 Button action some processes Image systemName circle
  • 为什么 numpy 比 python 慢?如何让代码执行得更好

    我将我的神经网络从纯 python 重新编写为 numpy 但现在它的运行速度甚至更慢 所以我尝试了这两个功能 def d a 1 2 3 4 5 b 10 20 30 40 50 c i j for i j in zip a b retu
  • (远程)状态文件中的 Terraform 和明文密码

    Terraform 存储库上有许多关于此问题的 Git 问题 其中有很多有趣的评论 但截至目前我仍然没有看到此问题的解决方案 Terraform 将纯文本值 包括密码 存储在 tfstate 文件中 大多数用户都需要远程存储它们 以便团队可
  • Kafka Mirror Maker 无法复制 __consumer_offset 主题

    我正在尝试利用镜子制造商来复制 consumer offsets主题与其他主题一起 它给出了如下所述的错误 2018 10 24 16 16 03 802 ERROR 向主题发送消息时出错 consumer offsets 键 16 字节
  • 仅跟踪 iframe 历史记录

    我有一个包含 iframe 的页面 我只想跟踪 iframe 的历史记录 我尝试像这样使用历史对象
  • 动态添加侦听器到 Google 地图标记

    我正在开发一个页面 该页面使用 Javascript httpObject 获取代码并使用它来更新页面上的两个元素 一个谷歌地图和一个列出标记指向的内容的 DIV 那一点效果很好 问题是 当我创建标记时 我通过 for 循环来完成此操作 并
  • 在管道操作符时如何返回可观察的“forkJoin”

    在我拥有这个运行良好的解析器之前 resolve return forkJoin this getData1 this getData2 this getData3 现在我必须做一些实际上行不通的事情 resolve return this
  • 在构建器模式中管理订单的首选方法是什么?

    我创建了一个流畅的构建器样式模式来帮助加载测试数据 某些方法的顺序很重要 并且想知道管理正确顺序的首选方法是什么 我目前有以下情况 using NUnit Framework TestFixture public class DataBui