Head First Design Pattern study notes – Proxy mode (1)
|
|
Proxy mode is a very common pattern.
Many of us use the passive mode, such as the use of Web Service,. Net Remoting, etc.. Common are the Remote Proxy, Virtual Proxy, Protection Proxy, Dynamic Proxy, this will introduce Remote Proxy and Virtual Proxy.
First, the Remote Agent (Remote Proxy):
Remote agent is such an object: When the client’s request arrives, the request will be passed to the remote agent first, and then passes the request by the remote agent to process the request to the real object, and this object may be located in another machine. However, this process is the client who is transparent, in their view, their use is the real object.
Due to the cross-machine call the object’s method, and therefore did not like using ordinary objects, we must consider how to create objects on a remote machine, how to make remote object know which method to execute, how to pass parameters, how to pass the return value, etc. . Fortunately, there are many existing technologies can solve these problems for us, such as Web Service,. Net Remoting, WCF and so on. Let’s look at a Remoting example:
1. The definition of a remote object:
{Remote.Interface}
public interface ICalculator
{
int Add (int x, int y);
}
{Remote.Imp}
public class Calculator: MarshalByRefObject, ICalculator
{
public int Add (int x, int y)
{
return x + y;
}
}
2. Server-side:
{Remote.Server}
class Program
{
static void Main (string [] args)
{
ChannelServices.RegisterChannel (new TcpChannel (10005));
RemotingConfiguration.RegisterWellKnownServiceType (typeof (Calculator), “Calculator.rem”, WellKnownObjectMode.Singleton);
Console.ReadLine ();
}
}
Server-side includes the following steps:
(1). Up channel: Channel used to transfer data
(2). Registration Service Type:
RegisterWellKnownServiceType first parameter is the type to be registered
The second parameter combination of a channel previously defined address of the service, in this case: tcp: / / [server ip]: 10005/Calculator.rem, the address registered for the type of client for use.
3. Client:
{Remote.Client}
static void Main (string [] args)
{
var calculator =
Activator.GetObject (typeof (ICalculator), “tcp: / / 127.0.0.1:10005 / Calculator.rem”) as ICalculator;
if (calculator! = null)
{
Console.WriteLine (calculator.Add (1, 2));
}
Console.ReadLine ();
}
The client is relatively easy, just use Activator.GetObject can get up after the type of object to use as a local object was obtained.
We do not have to define their own proxy object, which by the framework for us to complete, so that we often use the passive mode.
Download the complete code
Second, the virtual agent (Virtual Proxy)
When a resource or an operation is very time-consuming, we can define the resources or the operation of a proxy. Let the agents ‘false’ agency resources or the agent until you really need or the resources are available to use it again when the agent thing.
The following example loads a picture to illustrate:
1. Define an interface:
public interface IImageLoader
{
void LoadImage ();
}
2. The definition of the real loader
public class ImageLoader: IImageLoader
{
private string imgUrl = string. Empty;
private PictureBox pb;
public ImageLoader (string imgUrl, PictureBox pb)
{
this. imgUrl = imgUrl;
this. pb = pb;
}
# Region IImageLoader Members
public void LoadImage ()
{
var request = WebRequest.Create (imgUrl);
pb.Image = Image.FromStream (request.GetResponse (). GetResponseStream ());
}
# Endregion
}
When the image is loaded when the display picture control to set the picture property. But the load image is a time consuming process, so in general we choose to first show that the image is loading, or prompts.
3. The definition of agent:
public class ImageLoaderProxy: IImageLoader
{
private ImageLoader loader;
private PictureBox pb;
public ImageLoaderProxy (string imgUrl, PictureBox pb)
{
loader = new ImageLoader (imgUrl, pb);
this. pb = pb;
}
# Region IImageLoader Members
public void LoadImage ()
{
new Thread (() =>
{
pb.Image = Image.FromFile (“loading.gif”);
loader.LoadImage ();
}). Start ();
}
# Endregion
}
Local agent will show a picture, then go load the pictures on the network.
Before the completion of loading:
After:
Download the complete code
Sorry, the comment form is closed at this time.


Comments
No comments yet.