dimanche 18 octobre 2015

Looking for a Design pattern which can create different instances of a class with different interface implementations

I have a class which contains a view dependencies (all interfaces). Basically the behavior of the class is defined through the implementation of those interfaces. I want to be able to have a "builder" which can create instances of this class with different implementations of the interfaces(or parts of it). Something like this:

public class API
{
 private readonly ISomeInterface _someInterface;
 private readonly ISomeOtherInterface _someOtherInterface;
 private readonly ISomeAnotherInterface _someAnotherInterface;

API(ISomeInterface someInterface,ISomeOtherInterface someOtherInterface,ISomeAnotherInterface someAnotherInterface)
{*/implementation ommitted*/}

//Example method
public void DoSomethingWhichDependsOnOneOrMoreInterfaces()
{
  //somecode
  id(_someInterface != null)
     _someInterface.SomeMethode();
}


public class MyApiBuilder()
{
  // implementation ommitted
  API CreateAPI(someEnum type)
  { 
    switch(type)
    {
       case SpecificAPI32:
            var speficImplementationOfSomeInterface = new ImplementsISomeInterface();
            speficImplementationOfSomeInterface .Setup("someSetup");
            var specificImplementationOfOtherInterface = new ImplementsISomeOtherInterface();
            returns new API(speficImplementationOfSomeInterface,specificImplementationOfOtherInterface ,null);

    }
  }
}

What is the most elegant way of implementing this (if this makes sense at all)? I was first thinking of the Builder Design Patterns but as far as I understood it, its slightly different.

Aucun commentaire:

Enregistrer un commentaire