3

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 Method of A and B. Any other solution?

------------Edit---------------

I don't want my Method to get any extra parameter. Under such circumstances, isn't static util class a better option than defining an abstract class?

1
  • 3
    maybe use an abstract class instead of an interface?
    – John Boker
    Oct 1, 2015 at 19:43

3 Answers 3

3

You can create a shared abstract base class which takes a T generic parameter and implements I<T>. That same T will be passed to Method, which will be implemented in the base class:

public interface I<T>
{
    void Method(T t);
}

public abstract class Base<T> : I<T>
{
            public Base(T t)
            {
                  this.param = t;
            }

            private readonly T param;
    public void Method()
    {
        // Do stuff
    }
}

public class A : Base<Folder>
{
          public A(Folder folder) : base(folder)
          { }
}

public class B : Base<List>
{
          public B(List list) : base(list)
          { }
}

public class Folder { }
public class List { }

Now, you can do:

static void Main()
{
    var a = new A(new Folder());
    a.Method();

    var b = new B(new File());
    b.Method();
}
10
  • thanks, however I don't want my method to get any extra parameter.
    – HHH
    Oct 1, 2015 at 20:50
  • @H.Z I've edites the code. You can take the T param at construction time. Oct 1, 2015 at 20:55
  • one more thing, can I also change some pat of my implementation logic in the base class depending on the type of T (e.g. if (T is Folder)))?
    – HHH
    Oct 1, 2015 at 20:57
  • If you need to do that, you should start questioning the use of generics at all, and pursue a different path. What do you need to change based on T? Oct 1, 2015 at 20:59
  • so what is the best option then ?
    – HHH
    Oct 1, 2015 at 21:00
0

One option is to use an abstract class instead of an interface. Of course this class must be generic.

Second option is to extract method to "static utils" class and in both use this "utils". But this assumes that list and folder are instances of same interface.

In my opinion the first option is the best one.

1
  • could you have a look at the 'Edit' I just added to my original post?
    – HHH
    Oct 1, 2015 at 20:54
0

If the method's implementations differ only by a variable's type consider using Template Method pattern combined with a generic base class.

abstract class Base<T> : I
{
   public void method()
   {
       var items = GetItems();
       HandleItems(items);
   }

   protected abstract IEnumerable<T> GetItems();

   void HandleItems(IEnumerable<T> items)
   {
       // do something with a sequence
       foreach(var item in items) Console.WriteLine(item);
   }
}

class A : Base<FolderItem>
{
    Folder folder;

    protected overrides IEnumerable<FolderItem> GetItems() {return folder.Items;} 

}

class B : Base<ListItem>
{
    List list;

    protected overrides IEnumerable<ListItem> GetItems() {return list.Items;} 
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.