views:

153

answers:

5

I have 80-90 classes which are all a type of calculation. But each class uses one method compute which is the same in each in class. The items that are different in each class are the are the instance variables used inside the compute method.

The reason I am looking for a pattern is because what I am doing seems extremely repetitive.Below is an example of just one calc but there can be up to 200 calculations.

Protected Overrides Sub Compute(ByVal cache As Cache.ClientCache(Of System.Guid), _
                                 ByRef objIsTickBoxATicked As IClientAnswer(Of System.Guid))
     Dim objClientAdviceParas As ClientAdviceParagraphs

     'Get the Client Advice Paragraphs
     objClientAdviceParas = GetTickBoxesFromCache(GetAnonymousCache(cache), _
                                                  CType(cache.Client, Client))
     'Return the value
     objIsTickBoxATicked.BoolValue = _
                CheckTickBox(objClientAdviceParas, m_csClientAdviceParaWelfareBenefitsReferral)

End Sub
+4  A: 

Have you considered creating a single superclass for your 80-90 classes? You could put the common logic in the superclass.

ctford
+2  A: 

I think you should consider the Template Method Pattern.

Jim G.
+2  A: 

I am a bit confused. You say, that the method that does the computation is the same in each class, but it uses different instance variables. Does that mean that the method always has the same signature, but the implementations differ? In that case, all you could do would be to factor out the method and the common member variables into a superclass, but you would still have to write an implementation of the calculation method for each calculation. This does not save you much work, but it enables you to use calculations polymorphically. Generally, if each calculation is different (even just a little bit) you will have to implement it independently. The Template Method Pattern helps if all calculations have a common pattern, e.g.:

1. fetch values
2. apply transform
3. sort result

or something like that.

Space_C0wb0y
+1  A: 

As you describe that only "instance variables" are different in your 80-90 classes, I would recommend making them one and only one class. It is the responsibility of instances to carry different values of instance variables, not classes.

If the code to create an instance is complex, you can then create a Factory class, with a separate factory method for each case: createXXX(). All these methods would return a new instance of the same class, initialized with different values.

Eric Bréchemier
A: 

Perhaps the problem is a misalignment of the problem's "language" and the programming language. That is, it sounds like you are using an object-oriented approach to solve a functional problem.

Use a functional language.

Doug Knesek
I have looked at the template method pattern, but it still means I have 70-80 different classes. With just the one method. A functional programming language is something I considered a while ago, or even creating a DSL so the analysts providing the calculations can just write the calculations themselves.I know enough about functional languages to realise it can do, what I am wanting. Implementing it will be another matter, thanks for sending me in the right direction.
Miker169