简体   繁体   English

这是一种设计模式吗?

[英]is this a design pattern?

i have to build some financial data report, and for making the calculation, there are a lot of 'if then' situations: if it's a large client, subtract 10%, if it's postal code equals '10101', add 10%, if the day is on a saturday, make a difficult calculation etc. 我必须建立一些财务数据报告,并且为了进行计算,有很多“如果那时”的情况:如果它是一个大客户,减去10%,如果它的邮政编码等于'10101',加10%,如果这个星期六是星期六,难以计算等等。

so i once read about this kind of example, and what they did was (hope i remember well) create a class with some base info and make it possible to add all kinds of calculationobjects to it. 所以我曾经读过这样的例子,他们所做的是(希望我记得很清楚)创建一个带有一些基本信息的类,并且可以为它添加各种计算对象。

So to put what i remembered in pseudo code 所以把我记得的东西放在伪代码中

Basecalc bc = new baseCalc();
//put the info in the bc so other objects can do their if
bc.Add(new Largecustomercalc());
bc.Add(new PostalcodeCalc());
bc.add(new WeekdayCalc());

the the bc would run the Calc() methods of all of the added Calc objects. bc将运行所有添加的Calc对象的Calc()方法。 As i type this i think all the Calc objects must be able to see the Basecalc properties to correctly perform their calculation logic. 当我键入它时,我认为所有Calc对象必须能够看到Basecalc属性才能正确执行其计算逻辑。

So all the if's are in the different Calc objects and not ALL in the Basecalc. 因此所有if都在不同的Calc对象中,而不是在Basecalc中的所有。

does this make sense? 这有意义吗?

I was wondering if this is some kind of design pattern? 我想知道这是否是某种设计模式?

As dtb suggested, Chain of Responsibility seems most applicable here, with a slight variation: Typically, Chain of Responsibility finds exactly one handler , then exits. 正如dtb建议的那样, 责任链似乎最适用于此,略有不同:通常,责任链只找到一个处理程序 ,然后退出。 If a large customer ordered on Saturday, you'd need to execute two handlers. 如果周六有大客户订购,则需要执行两个处理程序。 Note that doing so is a non-trivial extension, because your object might have changed in the mean time and the ordering of handlers becomes relevant. 请注意,这样做是一个非平凡的扩展,因为您的对象可能在平均时间内发生了变化,并且处理程序的顺序变得相关。 This can be very tricky. 这可能非常棘手。 Eg what if there is a $10 discount and a 10% discount? 例如,如果有10美元的折扣和10%的折扣怎么办? Now the order of operations makes a difference, unless both work on the original price. 现在,操作顺序有所不同,除非两者都按原价运行。 You get the idea I guess. 你猜对了。

It is important to realize that design patterns aren't clear-cut, so there is typically not the one correct answer. 重要的是要意识到设计模式并不明确,因此通常没有一个正确的答案。 Still I believe this is very close to Chain of Responsibility and further from the other patterns that have been mentioned. 我仍然相信这与责任链非常接近,并且与已经提到的其他模式相比更进一步。

First, it is desirable to have concrete implementations perform the check whether they are actually applicable to the item at hand, which is typical for Chain of Responsibility. 首先,希望具体实现执行检查它们是否实际适用于手头的项目,这对于责任链来说是典型的。

Secondly, what you need will behave differently depending on the actual data in your object. 其次,根据对象中的实际数据,您需要的行为会有所不同。 The Strategy pattern simply encapsulates different algorithms that essentially achieve the same thing (ie you could have different strategies to calculate a 10% discount, but they should all yield the same value). 策略模式简单地封装了基本上实现相同目标的不同算法(即,您可以使用不同的策略来计算10%的折扣,但它们都应该产生相同的值)。

The Command pattern is a decoupling pattern for the actual request to perform an operation, eg if you wanted to have somebody else calculate the discount, you'd spawn a Command object for that. Command模式是执行操作的实际请求的解耦模式,例如,如果您想让其他人计算折扣,您将为此生成一个Command对象。 In fact, the handlers of (multicast) events are often Chains of Responsibility . 实际上,(多播)事件的处理程序通常是责任链

The Composite pattern is made for tree-like structures where you can compose objects much like you would in the real world. Composite模式是为树状结构而制作的,您可以像在现实世界中一样组合对象。 This is related to the issue mentioned before: If you represent your Chain of Responsibility as a degenerate subtree (without branches), you'll have an ordered list and you can represent the difference between subtract $10 first, then 10% or the other way around. 这与之前提到的问题有关:如果您将责任链表示为退化子树(没有分支),您将拥有一个有序列表,您可以表示先减去$ 10,然后减去10%或另一种方式之间的差异周围。 In this sense, it could be understood as a highly degenerate Composite. 从这个意义上说,它可以被理解为一种高度退化的复合材料。 The Composite could be used to describe a specific discounting scheme. Composite可用于描述特定的折扣方案。 Still, selecting and applying the scheme would be Chain of Responsibilities' job. 仍然,选择和应用该计划将是责任链的工作。

Like I said, these are not clear-cut terminologies, they are strongly related to each other, often found in variations (and in abuse) and, most importantly, every pattern needs some alterations for your specific problem. 就像我说的那样,这些并不是明确的术语,它们彼此密切相关,经常出现在变体(和滥用)中,最重要的是,每个模式都需要对您的特定问题进行一些更改。 Still, I'd prefer to stick to the terminology of Chain of Responsibility with a few variations, because I believe it's closest to the situation you describe. 尽管如此,我仍然倾向于使用一些变体来坚持责任链的术语,因为我认为它最接近你描述的情况。

Seems like the Strategy pattern for me. 对我来说似乎是战略模式 Mixed with the Composite pattern perhaps, as you can add the Calculation implementations to your object. 也许与Composite模式混合,因为您可以将Calculation实现添加到对象中。

There seem to be two patterns at work there, depending on what you're looking at. 在那里似乎有两种模式,取决于你正在看什么。 If you're talking about the work delegation to a list of items via a standard "calculate" method, then that's an example of Command pattern . 如果您通过标准的“计算”方法谈论工作委托到项目列表,那么这就是Command模式的一个例子。 If you're talking about the implementation of the calculator classes, that's Decorator pattern . 如果你在谈论计算器类的实现,那就是Decorator模式

It is not chain of responsibility, chain of responsibility has a complete different purpose. 它不是责任链,责任链有着完全不同的目的。 It is a combination of command pattern (without undo/redo) and composite (composing commands). 它是命令模式(没有撤消/重做)和复合(组合命令)的组合。 As others pointed out you can also consider it being part in a Strategy, but again it is to degenerated for being a good example for a Strategy. 正如其他人所指出的那样,你也可以认为它是战略的一部分,但同样也是为了成为战略的一个好榜样而堕落。 Some people would perhaps call it an "internal DSL". 有些人可能会称之为“内部DSL”。 As most people pointed out: you can not really define what it is, as it depends on your point of view. 正如大多数人所指出的那样:你无法真正定义它是什么,因为它取决于你的观点。 If your calculations had more than one method and the traversal algorithm would call all of them - or a subset depending on the operants eg - it would be Strategy, the only thing that is certain is the "Composite". 如果你的计算有多个方法,并且遍历算法会调用所有这些 - 或者是一个取决于操作符的子集,例如 - 它将是策略,唯一可以肯定的是“复合”。

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

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