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.