mardi 25 septembre 2018

Design Pattern - Is generic applicable?

Current classes

AMetric{ field1; field2.. etc }

AMetricGetter{

       AMetric getMetric(A aobject) 
       // A class contains same fields as AMetric class.                
       // Ametric class contains a few extra fields which are calculated in this method.
}

Now the problem is, a new class has to be created called BMetric which has a few fields similar to AMetric but is totally different type so no inheritance between them.

What i dont want to do is copy the whole code structure above for class B, as there is significant overlap here.

Class A and B also share some fields, but also have different fields, so theres no inheritance between them.

I thought of using generics

 getMetric(T tobject)

where tobject can either be class A or class B, but it doesnt help as some fields are specific to A or B, so i have to know the type of T in order to do transformation to AMetric or BMetric.

Any other design pattern?

Aucun commentaire:

Enregistrer un commentaire