C#设计模式建议 [英] C# design pattern suggestions

查看:129
本文介绍了C#设计模式建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象的集合。在这个集合中,我需要使用一些条件来搜索对象的出现。



使用条件1搜索



如果条件1无法使用条件2



如果条件2失败使用条件3



如果条件3无法使用条件4



每个条件都由多个过滤器组成。



我正在寻找关于可维护的设计模式的建议。

解决方案

看起来像责任链:



http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern


在面向对象设计中,责任链模式是一种
设计模式,由命令对象和一系列
的处理对象。每个处理对象包含
定义它可以处理的命令对象的类型的逻辑;其余的是
传递给链中的下一个处理对象。一个机制还有
,用于将新的处理对象添加到此链的末尾。


不要挂断很多关于命令对象的事情。 CoR模式的核心在于它是一系列对象,可以自己处理工作,也可以将其传递给链中的下一个对象。



实现:

  public interface LinkInChain {
boolean搜索(最终数据数据,最终OnFound onFound);
}

public abstract class LinkInChainBase {
final private LinkInChain nextLink;

public LinkInChainBase(final LinkInChain nextLink){
this.nextLink = nextLink;
}

protected abstract innerSearch(final Data data,final OnFound onFound);

public boolean search(final Data data,final OnFound onFound){
if(!innerSearch(data,onFound)){
return nextLink.search(data,onFound);
}
}
}

public class SearchFactory {

private final LinkInChain lastLink = new LinkInChain(){
public布尔搜索(最终数据数据,最终OnFound onFound){
return false;
}

}

public LinkInChain searchChain(){
返回新的SearchUsingCond1(
new SearchUsingCond2(
new SearchUsingCond3
new SearchUsingCond4(
lastLink




}
};


I have a collection of objects. Out of this collection I need to search for an occurrence of an object using a number of conditions. ie.

Search using Condition 1

If Condition 1 Fails use Condition 2

If Condition 2 Fails use Condition 3

If Condition 3 Fails use Condition 4

Each of these conditions consists of a number of filters.

I'm looking for suggestions with regards to a design pattern that's maintainable. Sample implementations will be appreciated.

解决方案

It looks like Chain of Responsibility:

http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern

In Object Oriented Design, the chain-of-responsibility pattern is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain.

Don't get hung up too much on the "command objects" thing. The core of CoR pattern is that it's a chain of objects that either handle the work themselves, or pass it on to the next one in the chain.

Implementation:

public interface LinkInChain {
  boolean search(final Data data, final OnFound onFound);
}

public abstract class LinkInChainBase {
  final private LinkInChain nextLink;

  public LinkInChainBase(final LinkInChain nextLink) {
    this.nextLink = nextLink;
  }

  protected abstract innerSearch(final Data data, final OnFound onFound);

  public boolean search(final Data data, final OnFound onFound) {
    if (!innerSearch(data, onFound)) {
      return nextLink.search(data, onFound);
    }
  }
}

public class SearchFactory {

  private final LinkInChain lastLink = new LinkInChain() {
    public boolean search(final Data data, final OnFound onFound) {
      return false;
    }

  }

  public LinkInChain searchChain() {
    return new SearchUsingCond1(
      new SearchUsingCond2(
        new SearchUsingCond3(
          new SearchUsingCond4(
            lastLink
          )
        )
      )
    )
  }
};

这篇关于C#设计模式建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆