The name of this design pattern? A bit like Strategy and the Chain of Response

Does anyone know if this template has a name? I try to use it sufficiently.

This is basically a behavioral pattern that allows you to provide a method with enumerated classes that implement the interface, and it runs all of them. Here is a C # example:

interface IInputValidator { bool IsValid(int input); } class GreaterThanZeroValidator : IInputValidator { public bool IsValid(int input) { return input > 0; } } class LessThanOneThousandValidator : IInputValidator { public bool IsValid(int input) { return input < 1000; } } 

then a method that uses them:

 public void ValidateInput(int input, IEnumerable<IInputValidator> validators) { bool allValid = true; foreach(var validator in validators) { if(!validator.IsValid(input)) allValid = false; } if(!allValid) throw new ArgumentException(); } 

So, for me, this looks like a strategy template, but with a few strategies that everyone gets the opportunity to process input, while a regular Strat template accepts only 1 strategy.

It is also a bit like a chain of responsibility, except that in a regular CoR, only the iterators are deep enough to find one that can handle the input, and each responsibility has a link to the next responsibility (for example, a linked list), whereas in In my example, I pass them all together.

I just want to call it. Thanks for any help!

+4
source share
2 answers

IMHO this is the chain of responsibility. The reason is that the LINQ All extension ends when it reaches the first element, because of which the predicate returns false.

Here's the LINQPad code confirming my statement about All :

 void Main() { try { Console.WriteLine("all items are 2?"); Console.WriteLine(YieldOneThenThrow().All(i => i == 2)); Console.WriteLine("all items are 1?"); Console.WriteLine(YieldOneThenThrow().All(i => i == 1)); } catch (NotSupportedException) { Console.WriteLine("exception! second item was visited."); } } IEnumerable<int> YieldOneThenThrow() { yield return 1; throw new NotSupportedException(); } 

... and conclusion:

all items: 2?
False
all items 1?
an exception! The second item was visited.

+1
source

But yes, a strategy template has been developed, and consumption is a chain of responsibility.

It depends on what the user gets - if the user is provided with ValidateInput I will classify it as CoR. If the user is provided with only the first set of classes, I would call it "Strategy".

0
source

All Articles