"Java design mode" proxy mode

Source: Internet
Author: User

The core role of proxy mode is to control access to objects through the agent. This is the same as the actual, such as the star has a broker, this is an agent, such as someone to find a star filming, then the first deal with this matter is his agent, although filming needs to shoot, but before and after the filming of some must do things, and so on, by this agent to deal with.
The same is true in the program, through the agent, you can control access to a certain or some kind of object method, before calling this method to do pre-processing, call this method after the processing. This is also the implementation principle of AOP.
So how is the core role of the proxy model designed?

Design ideas: Define an abstract role that allows the proxy role and the real role to implement it separately.
1. Real role: Implement abstract roles, define the business logic to be implemented by the real role, for proxy role invocation . It focuses only on the real business logic, such as filming.
2. Proxy role: the implementation of abstract role, is the real role of the proxy, through the real role of the business logic approach to achieve abstract methods, and can attach their own operations , such as talking about contracts, layout venues and so on.

The following to implement this proxy mode, proxy mode is divided into static agents and dynamic agents. Static proxy is our own to create a proxy class, and dynamic agent is the program automatically help us generate a proxy, we do not have to control.

1. Static proxy

Based on the implementation steps above, first write an abstract role:

/** * @Description 接口类 * @author Ni Shengwu * */publicinterface Star {    publicvoidconfer//面谈    publicvoidsing//唱歌    publicvoidcollectMoney//收钱}

There are three methods in this abstract class, and now let the real and proxy roles implement the abstract class, respectively:

/** * @Description Real objects * @author Ni SHENGWU * */ Public  class Realstar implements Star {    @Override     Public void Confer() {System.out.println ("Realstar.confer ()"); }@Override     Public void Sing() {System.out.println ("Realstar (Jay Chou). Sing ()"); }@Override     Public void Collectmoney() {System.out.println ("Realstar.collectmoney ()"); }}

Singing part, note Jay Chou, and so will be good to distinguish. Here's another look at the proxy class.

/** * @Description proxy class * @author Ni SHENGWU * */ Public  class Proxystar implements Star {    PrivateStar Star; Public Proxystar(Star Star) {///When the real star is sent in        Super(); This. Star = Star; }@Override     Public void Confer() {System.out.println ("Proxystar.confer ()"); }@Override     Public void Sing() {//Other things are capable, the only thing can not do is singing, singing or Jay Chou himself to singStar.sing ();//Let him sing it himself.}@Override     Public void Collectmoney() {System.out.println ("Proxystar.collectmoney ()"); }}

In the proxy class, you can see that a star object is maintained, a real star object is assigned by means of a construction method, and then in the singing method, the real object is used to sing. So the interview, collect money is by the agent object to achieve, singing is proxy object let real object to do.
Here is a test case for the client:

publicclass Client {    publicstaticvoidmain(String[] args) {        new RealStar();        new ProxyStar(real);        proxy.confer();        proxy.sing();        proxy.collectMoney();    }}

Output Result:

Proxystar.confer ()
Realstar (Jay Chou). Sing ()
Proxystar.collectmoney ()

It can be seen that the client only deal with proxy objects, proxy objects can do all do, such as interview and collect money, singing, is to call the real object to sing.

2. Dynamic Agent

Dynamic proxy is more widely used than static proxy, in essence, the proxy class does not need us to control, we give the tool to generate proxy class completely. Star interface and Realstar implementation class or the same as above, the following to define the proxy class, the use of dynamic proxy needs to implement the Invocationhandler interface, and overwrite the Invoke method inside, as follows:

/** * @Description Dynamic proxy class * @author Ni SHENGWU * */ Public  class Starhandler implements Invocationhandler {Star Realstar; Public Starhandler(Star Realstar) {Super(); This. Realstar = Realstar; }@Override     PublicObjectInvoke(Object Proxy, Method method, object[] args)throwsThrowable {Object object =NULL;//We can do our own work before acting on the real object .System.out.println ("Interview");if(Method.getname (). Equals ("Sing") {object = Method.invoke (Realstar, args); }//After the agent is really the object, we can do some of our own operationsSystem.out.println ("Collect Money");returnObject }}

It can be seen that the idea and the static proxy is the same, first through the construction method of the real object passed in, and then the execution of the proxy part is the Invoke method, in this method, we make a judgment, only when the need to sing, I call just passed in the real object to sing, Other things are done by proxies instead of real objects, which are only simulated with console output.
Finally, write a client-side test:

publicclass Client {    publicstaticvoidmain(String[] args) {        new RealStar();        new StarHandler(realStar);        //创建代理类        new Class[]{Star.class}, handler);               proxy.sing();    }}

Look at the console output:

Interview
Realstar (Jay Chou). Sing ()
Collect money

This shows that using a proxy can add new logic to the execution of a certain logic, which is a good feature that is actually used by spring's AOP.
  

-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15

"Java design mode" proxy mode

Related Article

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.