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??
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
implementedIList<User>
interface, it would be a proxy forList<User>
. Similarly, ifListUsers
let you obtain a special subclass ofUser
that lets you read the data but not write it, that special class would be a proxy for theUser
..NET uses the proxy pattern in several notable places, such as managing list access through
List<T>.AsReadOnly
, or theSynchronized
wrappers ofHashTable
andArrayList
.