Saturday, November 13, 2010

..........Proxy Pattern

The Proxy pattern is used when you need to represent a complex object by a simpler one. If creating an object is expensive in time or computer resources, Proxy allows you to postpone this creation until you need the actual object. A Proxy usually has the same methods as the object it represents, and once the object is loaded, it passes on the method calls from the Proxy to the actual object.

There are several cases where a Proxy can be useful:
1. If an object, such as a large image, takes a long time to load.
2. If the object is on a remote machine and loading it over the network may be slow, especially during peak network load periods.
3. If the object has limited access rights, the proxy can validate the access permissions for that user.
4) In Remoting we use Proxy class that is a fake copy of server object that reside on client and call the real server object when we made a call to that proxy. this proxy help to compile client code as it is calling server method but server method are not present at compile time on client machine.


///
/// The 'Company' abstract class
///

abstract class Company
{
public abstract void GetDetail();
}

///
/// The 'MainClassEngineering' class
///

class MainClassEngineering: Company
{
public override void GetDetail()
{
Console.WriteLine("Called Main Class Method.");
}
}



///
/// The 'ProxyClass' class
///

class ProxyClass : Company
{
private MainClassEngineering _realObject;

public override void GetDetail()
{
// Use 'lazy initialization'
if (_realObject== null)
{
_realObject= new MainClassEngineering();
}

_realObject.GetDetail();
}
}

///
/// Entry point into console application.
///

static void Main()
{
// Create proxy and request a service
ProxyClass proxy = new ProxyClass();
proxy.GetDetail();
}

No comments:

Followers

Link