Question about maintainability design pattern

I'm not sure if there is a template that should be used here, but here is the situation:

I have a number of specific classes that implement the interface:

public interface IPerformAction { bool ShouldPerformAction(); void PerformAction(); } 

I have another class that checks input to determine if ShouldPerformAction should be executed. Rubbing is that new checks are added quite often. The interface for the validating class is defined as follows:

 public interface IShouldPerformActionChecker { bool CheckA(string a); bool CheckB(string b); bool CheckC(int c); // etc... } 

Finally, I currently have specific classes that call each of the validation methods with data specific to that particular class:

 public class ConcreteClass : IPerformAction { public IShouldPerformActionCheck ShouldPerformActionChecker { get; set; } public string Property1 { get; set; } public string Property2 { get; set; } public int Property3 { get; set; } public bool ShouldPerformAction() { return ShouldPerformActionChecker.CheckA(this.Property1) || ShouldPerformActionChecker.CheckB(this.Property2) || ShouldPerformActionChecker.CheckC(this.Property3); } public void PerformAction() { // do something class specific } } 

Now every time I add a new check, I have to reorganize specific classes to include a new check. Each particular class passes different properties to the validation method, so subclasses of specific classes are not an option. Any ideas on how this can be implemented in a cleaner way?

+6
c # design-patterns
source share
6 answers

The names "CheckA", "CheckB", etc., supposedly chosen to avoid the disclosure of confidential information, also obscure the nature of the relationship between the classes, so I will have to wing it.

However, this is almost a double dispatch , except that you are converting objects between them.

EDIT: Try to reproduce the double-send template "by book", instead of decomposing the average sending of objects. To do this, you will need the following:

 public interface IPerformAction { bool ShouldPerformAction(IShouldPerformActionChecker checker); void PerformAction(); } public interface IShouldPerformActionChecker { bool CheckShouldPerformAction(FloorWax toCheck); bool CheckShouldPerformAction(DessertTopping toCheck); // etc... } public class FloorWax : IPerformAction { public string Fragrance { get; set; } // Note that the text of this method is identical in each concrete class, // but compiles to call a different overload of CheckShouldPerformAction. public bool ShouldPerformAction(IShouldPerformActionChecker checker) { return checker.CheckShouldPerformAction(this); } } public class DessertTopping: IPerformAction { public string Flavor { get; set; } // Note that the text of this method is identical in each concrete class, // but compiles to call a different overload of CheckShouldPerformAction. public bool ShouldPerformAction(IShouldPerformActionChecker checker) { return checker.CheckShouldPerformAction(this); } } public class ShimmerApplicationChecker : IShouldPerformActionChecker { // handles binding of FloorWax class to required checks public bool CheckShouldPerformAction(FloorWax toCheck) { return CheckAroma(toCheck.Fragrance); } // handles binding of DessertTopping class to required checks public bool CheckShouldPerformAction(DessertTopping toCheck); { return CheckAroma(toCheck.Flavor); } // some concrete check private bool CheckAroma(string aroma) { return aroma.Contains("chocolate"); } } 
+2
source share

Let's take a step back - why do you use interfaces in the first place? Is it possible to split a single implementation of IShouldPerformActionCheck between multiple implementations of IPerformAction ? The answer seems to be negative, because ICheck needs to know about the implementation-specific properties (Property1, Property2, Property3) for the Action to complete the check. Therefore, the relationship between IAction and ICheck requires more information than the IAction contract can provide ICheck. It seems that your validation classes should be based on specific implementations that are related to the specific type of action that they validate, for example:

 abstract class CheckConcreteClass { abstract void Check(ConcreteClass concreteInstance); } 
+3
source share

You can combine checks into one common method that takes an object:

 public interface IShouldPerformActionChecker { bool Check(object o); } 

Then enter a list of these checks in your specific class:

 public List<IShouldPerformActionCheck> ShouldPerformActionChecker { get; set; } 

Less secure type, but more flexible.

Instead of using IShouldPerformActionCheck you can use the Predicate <T> delegate, which does IShouldPerformActionCheck much the same thing.

+1
source share

When you create a new CheckN , you still have to implement it in each of your specific validation classes, no?

Or are you talking about refactoring your IPerformActions to actually trigger this check?

Why do you just have CheckAll that calls everything?

0
source share

Instead of trying specific classes to check if an action should be performed, there might be a more convenient way to organize these objects.

What if the controller really implemented IPerformAction and had a member of IPerformAction, which he would call if the action should be performed? Can this member be either another check in the chain, or the actual class that performs the action if all the criteria have been passed?

This may require you to refactor a bit, so that the logic for executing the action is contained in one class, while the specific data that will be used is in another (for example, as a command template) so that the checkers can do their job .

Thus, you can easily add another validation rule by simply inserting it into the β€œchain” of objects leading to the final action.

0
source share

You can try something like this:

 List<IShouldPerformActionChecker> checkers = new List<IShouldPerformActionChecker>(); //... add in each checker to try foreach(ConcreteClass objectToCheck in checkset) { bool isGood = true; foreach(IShouldPerformActionChecker checker in checkers) { isGood &= checker.DoCheck(objectToCheck.Property); if (!isGood) { break; } } //handle failed check here } 
0
source share

All Articles