Is this a pattern? Proxy/delegation of interface to existing concrete implementation

Posted by Ian Newson on Programmers See other posts from Programmers or by Ian Newson
Published on 2012-06-12T15:24:31Z Indexed on 2012/06/12 16:48 UTC
Read the original article Hit count: 210

I occasionally write code like this when I want to replace small parts of an existing implementation:

public interface IFoo
{
    void Bar();
}

public class Foo : IFoo
{
    public void Bar()
    {
    }
}

public class ProxyFoo : IFoo
{
    private IFoo _Implementation;

    public ProxyFoo(IFoo implementation)
    {
        this._Implementation = implementation;
    }

    #region IFoo Members

    public void Bar()
    {
        this._Implementation.Bar();
    }

    #endregion
}

This is a much smaller example than the real life cases in which I've used this pattern, but if implementing an existing interface or abstract class would require lots of code, most of which is already written, but I need to change a small part of the behaviour, then I will use this pattern.

Is this a pattern or an anti pattern? If so, does it have a name and are there any well known pros and cons to this approach?

Is there a better way to achieve the same result? Rewriting the interfaces and/or the concrete implementation is not normally an option as it will be provided by a third party library.

© Programmers or respective owner

Related posts about design

Related posts about object-oriented