Is this a design template?

I need to collect a report on financial statements, and there are many “if” situations for calculation: if this is a big customer, subtract 10% if the postal code is “10101”, add 10% if on Saturday, make a complicated calculation, etc. .d.

therefore, I once read about this example, and what they did was (I hope I remember well) create a class with some information base and make it possible to add all kinds of calculated objects to it.

So, to put what I remembered in the 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()); 

bc will run Calc () methods for all added Calc objects. Since I am typing this, I think that all Calc objects should be able to see Basecalc properties in order to correctly execute their calculation logic.

So, all ifs are in different Calc objects, not ALL in Basecalc.

it makes sense?

I was wondering if this is some kind of design template?

+4
source share
4 answers

As shown in dtb, the chain of responsibility is most applicable here with a slight change: usually the chain of responsibility finds exactly one handler , and then exits. If a large customer ordered on Saturday, you will need to execute two handlers. Please note that this is a non-trivial extension, because your object could be modified in average time, and the ordering of the handlers becomes relevant. It can be very difficult. For instance. What if there is a $ 10 discount and a 10% discount? Now the order of operations matters if both do not work at the original price. You guess, I think.

It is important to understand that design patterns are not clear, therefore, as a rule, more than one correct answer. However, I believe that this is very close to the chain of responsibility and further from the other patterns that were mentioned.

First, it is desirable for specific implementations to check whether they are really applicable to the element in question, which is typical of the Responsibility Chain.

Secondly, what you need will behave differently depending on the actual data of your facility. The strategy template simply encapsulates various algorithms that essentially achieve the same thing (i.e. you could have different strategies for calculating a 10% discount, but they should all give the same value).

A command template is a decoupling template for an actual request for an operation, for example. if you want someone else to calculate the discount, you would create a Command object for this. In fact, event (multicast) event handlers are often targets of responsibility.

Composite template created for tree structures, where you can create objects in the same way as in the real world. This is due to the problem mentioned above: if you present your Responsibility Chain as a degenerate subtree (without branches), you will have an ordered list, and you can imagine the difference between subtracting $ 10 first, then 10% or other way around. In this sense, it can be understood as a highly degenerate composite. Composite can be used to describe a specific discount scheme. However, the selection and application of the scheme will do the job of the chain of responsibility.

As I said, these are not clear terminologies, they are strongly related to each other, often found in variations (and abuse), and, most importantly, each model needs some changes for your specific problem. Nevertheless, I would prefer to stick to the terminology of the “Chain of Responsibility” with several options, because I believe that it is closest to the situation you describe.

+5
source

Sounds like a Strategy Template to me. Mixed with Composite pattern , perhaps because you can add Calculation implementations to your object.

+3
source

It seems that two templates work there, depending on what you are looking at. If you are talking about delegating work to a list of elements using the standard method of "calculation", then this is an example of "Command Template" . If you are talking about implementing calculator classes, the Decorator template .

+1
source

This is not a chain of responsibility; a chain of responsibility has a completely different purpose. This is a combination of a command template (no undo / redo) and compound (composing teams). As others pointed out, you can also assume that this is part of the Strategy, but again it is degenerated in order to be a good example for the Strategy. Some people might call this the “internal DSL”. As most people have pointed out: you cannot determine what it is, because it depends on your point of view. If your calculations had more than one method, and the bypass algorithm will call all of them - or a subset depending on the operands, for example. - it would be a Strategy, the only thing that is undoubtedly is Composite.

0
source

Source: https://habr.com/ru/post/1307234/


All Articles