Popular understanding of Proxy mode (static proxy)

Source: Internet
Author: User

There are two types of proxy modes: static agents and dynamic proxies.

Let's start off with a pattern, for example:

There is a team of very beautiful girls, they are all well-trained welcome miss. Usually busy training coupled with network and advertising investment, business is not good. So their boss proposed to find a etiquette company to cooperate, ask them to send their welcome to the job to do them. Just in a company has a reception of foreign guests activities, the event to a well-known etiquette company responsible, etiquette company notified the welcome Miss. When the foreign guests get off the band to play music, walk to the door of the company, welcome miss Welcome to greet. Now let's simulate the situation.

Equivalent to miss welcome (delegate Class) public class HelloWorld {public void SayHello () {System.out.println ("hello!");}} Equivalent to this etiquette company (proxy class) public class staticproxy{//holds the resources of Miss Welcome to private HelloWorld HelloWorld = new HelloWorld ();//Welcome Event public void SayHello () {//own band play System.out.println ("Do  something ...");//Welcome to Miss Helloworld.sayhello ();} public static void Main (string[] args) {staticproxy staticproxy = new Staticproxy (); Staticproxy.sayhello ();}}

But things are not going well , suddenly heard that these foreign guests will be Chinese, and the heavyweight characters are very fond of Chinese traditional culture. So the company asked etiquette company immediately replace Miss, please a team of Chinese clothes and good mandarin. Fortunately, the team's Welcome Lady is a tough character and can handle all kinds of scenes.

So we made the following changes:

The equivalent of Miss Usher's new squad public class HelloWorld2 {public void SayHello () {System.out.println ("Hello! ");}} Equivalent to this etiquette company public class staticproxy{//holds the resources of the second team of welcome miss HelloWorld2 HelloWorld2 = new HelloWorld2 ();//Welcome Event public void SayHello () {//own band play System.out.println ("Do  something ...");//Welcome to Miss Helloworld2.sayhello ();} public static void Main (string[] args) {staticproxy staticproxy = new Staticproxy (); Staticproxy.sayhello ();}}

Welcome the boss of the team and think, this is too troublesome, every time change to change the toss not, it is better to call some people, strengthen the training of diversity, so that their team can cope with various scenes, and then respond quickly according to demand.

Professional welcome Team Public Interface Ihelloworld {//prescribes what to do public void SayHello ();} First squad public class Helloworldimpl implements ihelloworld{//follow the rules to do     speaking English public void SayHello () {System.out.println (" Hello! ");}} Second squad public class HELLOWORLDIMPL2 implements Ihelloworld {//is also welcome   in Standard Mandarin @overridepublic void SayHello () { SYSTEM.OUT.PRINTLN ("Hello! ");}}

To modify the proxy class:

Equivalent to this etiquette company public class staticproxy{//holds the resources of the Welcome team private Ihelloworld Hello;  Public Staticproxy () {//Hello Welcome team ready to play, specifically where to view the case hello = new Helloworldimpl ();        Hello = new HelloWorldImpl2 ();} Welcome event public void SayHello () {//own band playing System.out.println ("Do something ...  ");//Welcome to Miss Hello.sayhello ();} public static void Main (string[] args) {//The company does not know that there is a welcome team presence   it only and agent (etiquette company) Contact Staticproxy staticproxy = new Staticproxy (); Staticproxy.sayhello ();}}

the modified code can be said to be a static agent, the design pattern advocated for the interface programming , instead of programming for implementation , This can be flexible. The first code can be said to be a proxy, but not a model, the pattern is good design, if the further emphasis on commonality and scalability, it will have to mention the JDK and cglib Dynamic Proxy.


Let's summarize the static proxy from the above:

Some classes because of their own limited functionality, require other classes to do some work to assist the completion of certain functions, although these ancillary work is necessary, but the delegation of the class does not need to do, one is that he has no resources, and the other is that it should focus on what they are good at. Such a number of proxy classes came into being, it specifically to do some pre-treatment and aftercare, so that the delegation of the class focus on their own things. In life there are many such examples, like the above welcome team, its team well-trained, ability is very strong, but it does not have the ability to pick up some big jobs, so he can go to find etiquette company, the two signed a cooperation agreement, let etiquette company go out to work, welcome part of the package to the team to do so. There are often behind the scenes for the star busy brokers, they are living agents.

Back in the world of our programmers, look at the well-known frameworks, like action callsin struts2 , lazy loading ofhibernate , Spring of the AOP , no use of proxy mode, although it is no longer a simple static proxy, but the idea is probably the case. When we use these frameworks, we only need to focus on our own business logic, and other repetitive and mechanical tasks that have to be done are given to these behind-the-scenes heroes.




Popular understanding of Proxy mode (static proxy)

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.