Extension objects pattern

Posted by voroninp on Programmers See other posts from Programmers or by voroninp
Published on 2013-04-27T08:22:58Z Indexed on 2013/06/26 22:29 UTC
Read the original article Hit count: 233

Filed under:
|

In this MSDN Magazine article Peter Vogel describes Extension Objects partten.

What is not clear is whether extensions can be later implemented by client code residing in a separate assembly.
And if so how in this case can extension get acces to private members of the objet being extended?

I quite often need to set different access levels for different classes.
Sometimes I really need that descendants does not have access to the mebmer but separate class does. (good old friend classes)

Now I solve this in C# by exposing callback properties in interface of the external class and setting them with private methods. This also alows to adjust access: read only or read|write depending on the desired interface.

class Parent
{
    private int foo;

    public void AcceptExternal(IFoo external)
    {
        external.GetFooCallback = () => this.foo;
    }
}

interface IFoo
{
    Func<int> GetFooCallback {get;set;}
}

Other way is to explicitly implement particular interface.

But I suspect more aspproaches exist.

© Programmers or respective owner

Related posts about c#

Related posts about design-patterns