当使用此类时,要强制执行类的方法执行的特定顺序.是否存在这样的设计模式?

dhe*_*dez 1 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() { ... }
}
Run Code Online (Sandbox Code Playgroud)

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

我想强制执行这个执行顺序:1-类初始化2-调用GetListOfCustomersToImport如果为真3调用CreateSalesforceJob如果为真4调用CreateJobBatches如果为真5调用CloseSalesforceJob 6-然后继续调用UpdateBatchesProcessingInfo直到所有批次状态都有值Completed,Failed

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

例如:

// add this variable to my class
bool processInitialized = false;
bool customerSumaryListRetrieved = false;
bool salesforceJobSuccessfullyCreated = false;
bool salesforceBatchesSuccessfullyCreated = false;
Run Code Online (Sandbox Code Playgroud)

a)方法GetListOfCustomersToImport实现

public bool GetListOfCustomersToImport()
{
    .....
    //at the end
    customerSumaryListRetrieved = true;
Run Code Online (Sandbox Code Playgroud)

}

b)方法CreateSalesforceJob

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

    //at the end
    // method implementation
    salesforceJobSuccessfullyCreated = true;    
}
Run Code Online (Sandbox Code Playgroud)

有一个更好的方法吗?设计模式还是已知的实现?

Ser*_*rvy 8

如果这是执行操作的唯一可接受的方法,那么编写一个完全相同的方法,并使其成为您的类的唯一公共方法.如果以任何其他方式调用它们是不可接受的,则这些其他方法都不应该是公开的.

  • 这假设您不需要在步骤之间执行任何其他操作 (3认同)