Design pattern----Proxy (proxy) mode

Source: Internet
Author: User
Design Patterns ----Proxy ( Agent ) Mode GOF :Provides a proxy for other objects to control access to this object. Not like flyweight. Proxy is itself the meaning of the agent. The reason for access control for an object is to use it when we really need it, or some function of the object. In a forum system (such as the very famous "Tianya"), browsing the forum has two basic users, one is registered users: can publish articles, modify their published articles, delete their own articles, reply to other people's articles and so on. The other is visitors (unregistered users): Only read the article permission.   In such a system, you can use proxy mode to control the permissions of both users. According to what I see in the various references, the proxy model has many variants, and he applies to many places.  In Gof's book, the following scenarios using proxy mode are described: 1. Remote Proxy: Provides local representation of an object in a different address space.  2. Virtual agent (Virtua Proxy): Create expensive objects as needed. For example, on the site to see pictures, usually first show small pictures, want to see a large picture when you can click on a small picture.  3. Protection agent (Protection proxy): Controls access to the original object. The protection agent is used when the object should have different access rights. For example, we mentioned above the forum system.  4. Smart Reference replaces a simple pointer that performs additional operations while accessing an object. His typical uses include a reference count to the actual object, so that when the object is not referenced, it can be automatically freed (also known as smart pointers.   This seems to have a specific explanation in C + +; When you first refer to a persistent object, load it into memory, check to see if it has been locked before accessing an actual object to make sure that the other object doesn't change him. Give a simple example to illustrate the proxy pattern. In the entertainment circle star to perform, to advertise these things are not the stars themselves and business talk, but there is an economic person, the economic person acting this star of all foreign affairs activities. such as a contract with a certain film company, or with a manufacturer to sign advertising ah what.   This economic person is an agent of the foot color, he and business negotiations when it is possible to let the star to perform or to shoot ads, if the negotiations do not he canceled the business and the star's contact activities, to a certain extent, his control star activities. Here's a look at the simple code:
Package Proxy;

Public abstract class Musicperson
... {
public abstract void Age ();
public abstract void Requital ();//Remuneration
public abstract void Date ();
}//End Class Musicperson

Star Appearances:
Package Proxy;

public class MusicStar extends Musicperson
... {
public void Date ()
... {
System.out.println ("The music person ' s date");
}//end F ()

public void Requital ()
... {
System.out.println ("The music person ' s retuital");
}//end g ()

public void Age ()
... {
System.out.println ("The Musicperson ' s Age");
}//end h ()

}//End Class MusicStar

The agent is here:
Package Proxy;

public class Musicstarproxy extends Musicperson
... {
private Musicperson implementation;

Public Musicstarproxy ()
... {
implementation = new MusicStar ();
}

public void Date ()
... {
Implementation.date ();
}//end Date ()

public void Requital ()
... {
Implementation.requital ();
}//end Requital ()

public void Age ()
... {
Implementation.age ();
System.out.println ("Sorry I can ' t tell you the MusicStar ' s age!");
}//end Age ()

}//End Class Musicstarproxy

Application of Porxy mode:
Package Proxy;

public class Proxypattern
... {

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.