c# - 使用哪些设计模式来实现该业务逻辑?

标签 c# design-patterns oop

我正在尝试确定用于业务 key 验证 Web 服务的最佳设计模式。基本逻辑流程编码如下。该程序将采用一个参数并使用一个字段来帮助确定在搜索可以找到此业务 key 的多个系统时要采用的路径。首先搜索System1,如果没有找到,再搜索System2和System3。 System1 搜索逻辑取决于传递到原始验证方法的参数中的字段。

我不太确定要使用哪种设计模式。看起来命令、责任链、模板方法都可以用在这里。

通过下面的实现,我发现以下问题:

  1. 每个 SearchSystemX 方法都需要知道在未找到业务键时返回 null,以便“控制”方法继续搜索其他系统。

  2. 每个 SearchSystemX 必须知道如何填充业务对象,目前仅通过简单的原始字符串实现,但这仅作为示例。

请告诉我你的想法。

public string Validate (string parms) {
 string returnValue = null;

 returnValue = SearchSystem1(parms);

 if (returnValue == null) {
  returnValue = SearchSystem2(parms);

  if (returnValue != null) {
   returnValue = SearchSystem3(parms);
  }
  else if (returnValue == null) {
   if (parms == "Criteria1") {
    returnValue = SearchSystem4(parms);

    if (returnValue == null) {
     throw new ApplicationException("ID Invalid");
    }
   }
   else if (parms == "Criteria2") {
    throw new ApplicationException("ID Invalid");
   }
  }
 }
 return returnValue;

private string SearchSystem1 (string parms) {
 string returnValue = null;

 if (parms == "Criteria1") {
  returnValue = SearchSystem1UsingColumn1(parms);
 }
 else if (parms == "Criteria2") {
  returnValue = SearchSystem1UsingColumn2(parms);

  if (returnValue == null) {
   returnValue = SearchSystem1UsingColumn4(parms);
  }
 }

 if (returnValue != null) {
  returnValue = FetchXYCoordinate(parms);
 }

 return returnValue;
}

谢谢!

最佳答案

Chain of responsability

Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those that it cannot to the next processing object in the chain

因此,您定义并抽象 SearchSystem (或 SystemSearch )并添加子类,如下所示:

class SearchSystem
{
    //link to the next in the chain 
    SearchSystem next;

    // Basic search, If cannot handle forward to the next.
    public Result search( Criteria c ) 
    {
        Result r = doSearch( c );

        if( r != null ) 
        {
            return r;
        }

        return next.search( c );
    }
    // subclass specific system search
    abstract Result doSearch( Criteria c );
}

class SearchSystemOne: SearchSystem 
{
    Result doSearch( Criteria c )
    {
        // do some system 1 speficif stuff 
        // and return either and instance or null
    }
}
class SearchSystemTwo: SearchSystem 
{
    Result doSearch( Criteria c )
    {
        // do some system 2 speficif stuff 
        // and return either and instance or null
    }
}
.... etc etc. 
// End of the chain
class SearchSystemOver: SearchSystem 
{
    public Result search( Criteria c ) 
    {
        throw new ApplicationException("Criteria not foud", c );
    }
    Result doSearch( Criteria c )
    {
       // didn't we throw exception already?
    }
}

实例化

SearchSystem one = new SearchSystemOne();
SearchSystem two = new SearchSystemTwo();
SearchSystem three = new SearchSystemThree();
SearchSystem four = new SearchSystemFour();
SearchSystem over = new SearchSystemOver();

并建立链条

one.next = two;
two.next = three;
three.next = four;
four.next = over;

最后搜索它。

SearchSystem searcher = one;

searcher.search( Criteria.addName("Oscar").addLastName("Reyes"));

关于c# - 使用哪些设计模式来实现该业务逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1615208/

相关文章:

c++ - 在 C++ 类继承中使用函数

c# - ASP.NET MVC : Requested registry access denied when creating performance counters

c# - 在 Forms-Authentication 中动态使用 cookie

java - 在运行时访问对象!

sql-server - 在数据库中创建新行后,我应该返回该行的主键还是数据传输对象?

java - 将构造函数隐藏在包之外

c# - 在 C# 中将一种类型转换为另一种类型

c# - C# 中的 volatile 变量赋值

c# - 需要考虑的应用安全问题

c# - Data Transfer Object 中的属性应该扩展外键还是简单地公开它们的主键