jeudi 1 octobre 2015

Best way to implement similar methods of two different classes?

I have two classes (A and B) which implements the same interface (I) in C#. The interface have 5 methods to implement. Implementations of two of those methods are almost the same in both A and B except each implemetation uses a different variable. Here is the abstract layout of my classes.

class A : I
{
     Folder folder;
     void method()
     {
        //implementation uses ``folder``
     }

class B : I
{
     List list;
     void method()
     {
        //implementation uses ``list``
     }
}

Because the implementation of Method is the same (except the one parameter) I want to have implement Method only once. What is the best solution according of design patterns? one simple option is to define a third class which implements Methodand takes one parameter (either list or folder) and then call it within the Mathod of A and B. Any other solution?

Aucun commentaire:

Enregistrer un commentaire