how is Proxy pattern defined?

415 Views Asked by At

from my understanding i implement the proxy pattern when i hold a reference to another type as a member in my Proxy class. i also need to provides an interface, identical to the subject type, and Controls access to the real object.

so, if my code was to look like this, when the List member is the subject type, would i have been implementing the proxy pattern correctly?

public class ListUsers
{
    public List<User> UserList { get; set; }

    .
    .
    // Ctor and other methods that dont expose the "List<Users>" properties..
    .
    .

    public void Add(User i_User)
    {
        // added logic and control.
        if (!this.UserList.Exists(x => x.Id == i_User.Id))
        {
            this.UserList.Add(i_User);
            this.SetUserPassword();
        }
        else
        {
            .
            .
            .
        }
    }
}

also, if my description i correct, would that make any class who has any kind of member into a proxy pattern class??

1

There are 1 best solutions below

2
On

No, this is not a valid implementation of a proxy pattern, because the crucial feature of that pattern implementation is missing: the proxy of an object needs to pretend it's the thing that it is proxying, either by providing the same interface, or by providing implicit conversions to the proxied object.

If ListUsers implemented IList<User> interface, it would be a proxy for List<User>. Similarly, if ListUsers let you obtain a special subclass of User that lets you read the data but not write it, that special class would be a proxy for the User.

.NET uses the proxy pattern in several notable places, such as managing list access through List<T>.AsReadOnly, or the Synchronized wrappers of HashTable and ArrayList.