• Home
  • Forum
  • Terms and Privacy
17ebook! free ebooks! free download!
Complete online surveys and earn cash from GlobalTestMarket


Search here...
     

Head First Design Pattern study notes – Proxy mode (1)

Posted by admin 17 February, 2011

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

Categories : book review Tags :

Comments

No comments yet.


Sorry, the comment form is closed at this time.

Subscribe via Email


Windows Touch Technics and Technology tablet Solution Manual Smart Security/Hacking Science Related Science/Engineering samsung Review Programming Phones phone Nokia Newspapers netbooks Netbook Mobile Medical/Medicine Laptops Laptop IT Certification Iphone Hobbies Health Graphics and Design Google Game For Women For Men features Ericsson Encyclopedias Economics and Finances Economics/Business deals DB Consumer Electronics Computer Related Computer Comics Apple android about
  • Categories

    • book review
    • Ebook Download
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Archives

    • May 2013
    • April 2013
    • March 2013
    • February 2013
    • January 2013
    • December 2012
    • November 2012
    • October 2012
    • September 2012
    • August 2012
    • July 2012
    • June 2012
    • May 2012
    • April 2012
    • March 2012
    • February 2012
    • January 2012
    • December 2011
    • November 2011
    • October 2011
    • September 2011
    • August 2011
    • July 2011
    • June 2011
    • May 2011
    • April 2011
    • March 2011
    • February 2011
    • January 2011
    • December 2010
    • November 2010
    • October 2010
    • September 2010
    • August 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
  • Recent posts

    • The TouchFLO navigation on the HTC Touch2 Silver makes life simple
    • Windows Phone (mango) creating splashes in Global Market of Smartphones
    • Main features of the PDA mobile phones
    • Advantages Of Windows 7 Ultimate And Laptops
    • Nokia Lumia 800 Deals: Mid-Range Segment Just Get Better
    • How come Windows Replacement Can not be Delayed
    • Google Mail For Mobile Phones
    • Jailbreaking and unlocking yours i-phones
    • HTC Touch2 – A World-beater
    • The San Diego Real Estate Bubble

Copyright © 2013 17ebook! free ebooks! free download! All right reserved

  • Home
  • Forum
  • Terms and Privacy