我有一个代表进程的类
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/