views:

106

answers:

4

Hi,

i want to implement a utility class which methods are internal steps of a validation process. Is there a pattern for this or should i use a totally different approach? Im open for suggestions. (I´m coding in abap but i dont think that is important)

Edit: Its no frontend validation of text, but a check if certain conditions are matched. (The parameter is actually a table. For each row i check if there are conditions matched as an example if there is a valid entry in an other db table.)

Somthing like this:

Class Validator 
{
   private bool flag_error;

   private Step1 ( var a, var b )
   {
     //do somthing ...
   }

   private Step 2 ( var a )
   { 
     //do somthing ...
   }

   private Step 3 ( var c )
   {
     //do somthing ...
   }

   static Check(var a, var b, var c)
   {
    Step1(a, b );
    Step2( a );
    Step3( c );
    return flag_error;
    }
}

Usage:

if (Validator.Check(a,b,c) )
{
 //do good stuff
}
else
{
 //do error handling
};
A: 

There is one patten that comes to mind. It is the template pattern

I would make an abstract class with Check implemented as above and the Step methods to be implemented by the concrete classes.

hhafez
I have thought about that, but i dont see the real use for that. My class is only used to do specific task, i dont think this will be reused to do the same steps with diffrent algorythms. I think if i use the template method i can´t add additional steps or? I must implement them once in the template method and again in the concrete class? So why not use only one class an add a method?
Richard
"I must impelement them moce in the template method and again in the concrete class" That is not correct you implement Check in the abstract class and the Steps in the concrete class. You don't implement the Steps in the abstract class. If you don't see the use then it is probably not suitable for this job. The only advantage is if the flow is always the same (ie Always step1,2,3 etc) but there are different ways to do the steps in different contexts
hhafez
A: 

I once wrote a visitor pattern, which worked quite nicely for separating the actual validation from the fields.

Seems rather complex to me and i dont see the real benefit over a simpler implementation. Maybe you can give me some hints.
Richard
A: 

Hi,

I use C# attributes for validation purpose. You can find a detailed article here. This helps you to implement validation by configuration over convention. Using this technique your validation routines become highly abstract and reusable.

this. __curious_geek
thx for the answer but i dont think thats what i need. With validation i mean that certain conditions are matched in a backend program. (Check if needed database entries are there etc etc)
Richard
+2  A: 

Design decisions really depend on the details. Will there be multiple validator algorithm implementations? Try a Strategy or Template Method pattern.

If you only need this one class performing these multiple steps, you've already implemented a pattern, Composed Method. Keep it simple. Don't add layers of complexity unless they're truly needed.

Kevin Swiber
Thx for that answer. I can work with that!
Richard