1

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.

[Edit] As pointed out, the way I am implementing it is a factory method but I am not fully satisfied with it. The API can contain a varity of different interfaces which can be totally independent of each other but some may depend on others.(but not mandatory) I would like to give the user (the developer using this "API") as much freedom as possible in creating the API he wants to use. Lets try to explain what I am basically up to: Let's say I am developing a plugin for a game engine which can post achievments and other stuff to various social media channels. So basically there could be a Interface which implements the access to twitter,facebook,youtube,whathever or some custom server. This custom server could need some kind of authentification process. The user should be able to build at start the API in a nice (hmm fluent is nice..) way. So basically something like this:

var myTotallyForMyNeedsBuildAPI = API.CreateCustomApi().With(Api.Twitter).And(Api.Facebook).And(Api.Youtube).And(Api.CustomServer).With(Security.Authentification);

I actually do not know how to make that fluent but something like this would be nice.

zlZimon
  • 2,334
  • 4
  • 21
  • 51
  • have you check Factory ? – Kevin Avignon Oct 18 '15 at 15:48
  • yea I checked factory method, which is basically what my example does right? But I guess I do not need static for the MyApiBuilder, but I was wondering if there is any more elegant method. – zlZimon Oct 18 '15 at 16:19
  • Depending on the scale of your application, you might want to look into Dependency Injection. – Nick Bailey Oct 18 '15 at 16:35
  • @zlZimon Interested, I think i get what you mean... maybe much easier for other to also follow if you give an example interface where you are clearly showing the two different implementations, I don't see the need for 3 being passed in, for illustration is it not better to just work with one for now, The more code you give as a sample the better to understand what you are wanting. Just sounds like you want to consume different implementations of a shared Interface. – Seabizkit Oct 18 '15 at 16:43

2 Answers2

1

What you have implemented is the Factory method pattern.

It's perfectly fine for what you are trying to do, but you could have a look at the other factory patterns (i.e. here) based on your context and how you think you're code will evolve in the future.

Anyway, I will also consider to not tie this three interface together in a single factory. If they are really so tighten together to be consumed together and built together, maybe they should not be three different interfaces in the first place, or at least all three implemented by the same class, so your factory will build the appropriate class with the proper implementation of these.

Probably what you are after is the Decorator pattern. In your API class you invoke each interface if they have been provided to the API instance, which is the behaviour of the Decorator pattern. With this pattern you obtain a modular implementation that allow you to add multiple behaviours to your API.

Community
  • 1
  • 1
AleFranz
  • 771
  • 1
  • 7
  • 13
  • was not me, but would be helpful if a reason was given for this mark down. Maybe not be the answer... but has some creditable info.... mark down has since disappeared.... – Seabizkit Oct 18 '15 at 16:34
  • well those interfaces are not actually tighten together, they just define the implementation of the API – zlZimon Oct 18 '15 at 17:55
  • Are you expecting to provide only some of the interfaces to a particular API instance or adding new ones in the future? I added a new paragraph to the answer. – AleFranz Oct 18 '15 at 18:42
1

It's a good practice to use Dependency Injection as you want to give the programmer the ability to compose the object with desired configuration.

Check MEF and Unity frameworks which are great for this job.

For example in Unity you can write this:

// Introducing an implementation for ISomeInterface
container.Register<ISomeInterface, SomeImplementation>();
// Introducing an implementation for ISomeOtherInterface
container.Register<ISomeOtherInterface, SomeOtherImplementation>();
// Introducing an implementation for ISomeAnotherInterface
container.Register<ISomeAnotherInterface, SomeAnotherImplemenation>();
container.Register<API, API>();

// and finally unity will compose it for you with desired configurations:
var api = container.Resolve<API>();

In this scenario the api will be composed with desired implementations.

mehrandvd
  • 8,806
  • 12
  • 64
  • 111