views:

125

answers:

2

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();
       }
    }
}
+1  A: 

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

ChaosPandion
@Chaos: Would you say that it is just a facade, or is it a adapator or proxy that is also a facade?
Paul Hadfield
@Paul - It depends on how much you simplified your example code. Also typically an adapter is required to work with an existing class that requires a certain interface. Is this the case?
ChaosPandion
@Chaos, no that isn't the case, the introduction of an interface is to faciliate mocking in the unit tests, the 3rd party object does not implement any interface itself. Thanks for your comments.
Paul Hadfield
+2  A: 

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.

Paul Rubel