简体   繁体   English

在这种情况下应使用哪种设计?

[英]Which design should be used in this scenario?

I have a business logic like below and I would like to know which design pattern should be used here. 我有一个类似下面的业务逻辑,我想知道这里应该使用哪种设计模式。

Basically I have an input and number of factories which creates objects which are derived from the same base class. 基本上,我有一个工厂的输入和数量,这些工厂创建了从相同基类派生的对象。

Input => factory1 => Output1 输入=> factory1 =>输出1

Input => factory2 => Output2 输入=> factory2 =>输出2

Input => factoty3 => Output3 输入=>事实3 =>输出3

........ ........

........ ........

The number of factories varies. 工厂数量各不相同。 This is the kind of logic which will be enclosed in a method which will create a collection of Output1, Output2... and returns it. 这种逻辑将包含在一个方法中,该方法将创建Output1,Output2 ...的集合并返回它。

Which is the right design pattern in this scenario ? 在这种情况下哪个是正确的设计模式?

Another most matching real time example.. 另一个最匹配的实时示例。

I have a filename/pattern and there are different Finders. 我有一个文件名/模式,并且有不同的查找器。 One for finding the word docs matching the pattern, one for finding the excel docs. 一种用于查找与模式匹配的单词docs,一种用于查找excel文档。 one for finding the ppt docs matching the pattern. 一种用于查找与模式匹配的ppt文档。 At the end all docs(word,xl,ppts) should be returned. 最后应返回所有文档(word,xl,ppts)。

It seems fairly simple as I see it and you may not need a design pattern. 就我看来,这似乎很简单,您可能不需要设计模式。

Lets call this object a Processor instead of Factory so as not to confuse with the Factory pattern. 让我们将此对象称为“处理器”而不是“工厂”,以免与“工厂”模式混淆。

So your code could be simply like this : 因此,您的代码可能像这样:

interface Input;
interface Output;
interface Processor {
  Output process (Input i);
}

MainClass {
  List<Processor> processors = initializeProcessors();

  List<Output> process(Input i) {
      List<Output> outputs = ...;
      foreach (Processor p in processors) {
          Output o = p.process(i);
          outputs.add(o);
      }
      return outputs;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM