c# - 当使用此类时,想要强制执行类的特定方法执行顺序。是否存在针对此的设计模式?

标签 c# oop design-patterns

我有一个代表进程的类

internal class IntegrationWithSalesforce
{
    public IntegrationWithSalesforce()
    { // Initialize internal variables }

    public bool GetListOfCustomersToImport() { ... }
    public bool CreateSalesforceJob() { ... }
    public bool CreateJobBatches() { ... }
    public bool CloseSalesforceJob() { ... }    
    public void UpdateBatchesProcessingInfo() { ... }
    public bool AbortJob() { ... }
}

方法应按特定顺序执行,直到您调用 CloseSalesforceJob。

我想强制执行这个执行顺序: 1-类初始化 2- 如果为真,则调用 GetListOfCustomersToImport 3- 如果为真,则调用 CreateSalesforceJob 4- 如果为真,则调用 CreateJobBatches 5- 调用 CloseSalesforceJob 6- 然后继续调用 UpdateBatchesProcessingInfo 直到所有批处理状态 有值(value)完成,失败

我的第一个想法是使用表示状态(或执行)的 bool 变量 并在调用方法时将与方法相关的设置为 true,或者如果方法不是下一个顺序,则抛出自定义异常 ProcessOrderExecutionException。

例如:

// add this variable to my class
bool processInitialized = false;
bool customerSumaryListRetrieved = false;
bool salesforceJobSuccessfullyCreated = false;
bool salesforceBatchesSuccessfullyCreated = false;

a) 方法 GetListOfCustomersToImport 实现

public bool GetListOfCustomersToImport()
{
    .....
    //at the end
    customerSumaryListRetrieved = true;

b) 方法 CreateSalesforceJob

public bool CreateSalesforceJob()
{
    if(!customerSumaryListRetrieved)
        throw new ProcessOrderExecutionException();

    //at the end
    // method implementation
    salesforceJobSuccessfullyCreated = true;    
}

有更好的方法吗?设计模式或已知实现?

最佳答案

如果这是执行该操作的唯一可接受的方式,那么编写一个方法来执行此操作,并使其成为您的类的唯一公共(public)方法。如果以任何其他方式调用它们是 Not Acceptable ,那么这些其他方法都不应公开。

关于c# - 当使用此类时,想要强制执行类的特定方法执行顺序。是否存在针对此的设计模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40850770/

相关文章:

php - php函数调用中的下划线

c# - 使用 LINQ(第 2 部分)选择单个列表的所有唯一组合,不重复

c# - 如何在asp :BoundField?中将日期时间更改为本地GMT日期时间

c# - Lambda 表达式 - 如何从 IEnumerable<Object> 向 where 子句提供值?

c# - 如何使用 C# 从 url 解析 Json 内容?

c# - OOP - 使用改变实现的方法和半固态的对象的正确设计

php - 在javascript函数参数中插入变量

java - 如何避免不同的方法循环同一个列表?

swift - 在 Swift 中编写一个通用的主题-观察者实现

java - 构建器模式适用于哪里