Miker169
Miker169

Reputation: 244

Looking for a Design Pattern

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

Upvotes: 0

Views: 301

Answers (5)

Doug Knesek
Doug Knesek

Reputation: 6727

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.

Upvotes: 0

user207968
user207968

Reputation:

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.

Upvotes: 1

Björn Pollex
Björn Pollex

Reputation: 76778

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.

Upvotes: 2

Jim G.
Jim G.

Reputation: 15365

I think you should consider the Template Method Pattern.

Upvotes: 2

ctford
ctford

Reputation: 7297

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

Upvotes: 4

Related Questions