Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which design pattern is this?

In a current (C#) project we have a 3rd party assembly that contains a non-interfaced connection object. Using IoC, etc we can inject this concrete instance into our code, but it is proving a nightmare to unit test, etc. We are using MoQ as our mocking framework so ideally could do with an interface to work off and we don't want to go down the route of using something like Moles because we would like to minimise technologies.

If we create a interface to mimic desired functionality of the 3rd party connection object and then create an implementor of that interface containing an instance of the 3rd party object then this will allow our code to work off the interface and both our IoC and unit tests will be happy. However in a discussion we've gone around in circles as to which design pattern it really is!

So the question is, "Is the situation described above and illustrated in code below a:"

  1. Adapter as we are providing a wrapper to existing functionality.
  2. Proxy as we are provding an interface to something else.
  3. Facade because as part of the process we will be providing a simplified interface to a larger object.

 

namespace ExampleCode
{
    public interface IConnector
    {
       void Open();
    }

    public class ConnectorWrapper : IConnector
    {
       ThirdPartyConnector _instance;

       public ConnectorWrapper(ThirdPartyConnector instance)
       {
          _instance = instance;
       }

       void Open()
       {
         _instance.Open();
       }
    }
}
like image 781
Paul Hadfield Avatar asked Sep 08 '10 13:09

Paul Hadfield


2 Answers

Quick answer, facade.

From my GoF

Adapter:

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Doesn't appear to be done for interoperability reason, gonna say no here.

Proxy

Provide a surrogate or placeholder for another object to control access to it.

Don't look good either, it's not an access issue.

Facade:

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

This looks more like it. You're using the interfaces to abstract the different implementations, testing and application.

like image 54
Paul Rubel Avatar answered Oct 21 '22 00:10

Paul Rubel


This is definitely a facade. I do this all the time to simplify over-engineered APIs.

like image 22
ChaosPandion Avatar answered Oct 20 '22 22:10

ChaosPandion