Design Mode (22) ----- proxy design mode ----- static proxy

Source: Internet
Author: User
I. Use inheritance to implement static proxy

 

1 package com.DesignPatterns.al.Dynamic1;2 3 public interface Moveable {4     public void move();5 }
Package COM. designpatterns. al. dynamic1; import Java. util. random; public class tank implements moveable {@ override public void move () {system. out. println ("tank moving"); try {thread. sleep (new random (). nextint (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}
Package COM. designpatterns. al. dynamic1;/*** use inheritance to calculate the running time of move in tank ** @ author qingruihappy * @ data 11:19:43 on January 1, October 31, 2018 * @ note: */public class tank1 extends tank {@ override public void move () {long starttime = system. currenttimemillis (); super. move (); long endtime = system. currenttimemillis (); system. out. println ("the total time spent on tank is" + (endtime-starttime ));}}
1 package COM. designpatterns. al. dynamic1; 2/** 3 * implement static proxy with inheritance 4 * @ author qingruihappy 5 * @ data November 1, 2018 1:05:36 6 * @ note: 7 */8 public class test {9 public static void main (string [] ARGs) {10 moveable M = new tank1 (); 11 m. move (); 12} 13 14}

 

2. Use aggregation to implement static proxy

 

package com.DesignPatterns.al.Dynamic2;public interface Moveable {    public void move();}

 

 

Package COM. designpatterns. al. dynamic2; import Java. util. random; public class tank implements moveable {@ override public void move () {system. out. println ("tank moving"); try {thread. sleep (new random (). nextint (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}

 

 

Package COM. designpatterns. al. dynamic2;/*** aggregation implementation: Aggregation: there is another class implementation in one class. From the perspective of proxy, the current class is the proxy class, and the other class is the target class, the method of the target class is called when the proxy class is called. * There are tank classes in tank2, and the operations are tank-related methods. ** @ Author qingruihappy * @ data October 31, 2018 11:26:45 * @ Note: */public class tank2 implements moveable {tank T; Public tank2 (tank T) {super (); this. T = T ;}@ override public void move () {long starttime = system. currenttimemillis (); T. move (); long endtime = system. currenttimemillis (); system. out. println ("the total time spent on tank is" + (endtime-starttime ));}}
package com.DesignPatterns.al.Dynamic2;public class Test {    public static void main(String[] args) {        Moveable m=new Tank2(new Tank());        m.move();    }}

 

Iii. Comparison of inheritance and aggregation

 

package com.DesignPatterns.al.Dynamic3;public interface Moveable {    public void move();}
Package COM. designpatterns. al. dynamic3; import Java. util. random; public class tank implements moveable {@ override public void move () {system. out. println ("tank moving"); try {thread. sleep (new random (). nextint (1000);} catch (interruptedexception e) {e. printstacktrace ();}}}
Package com. designpatterns. Al. dynamic3;/*** now let's take a look at why implementation (aggregation) is used without inheritance. * The most general statement is to implement decoupling, because the essence of the design pattern is abstract programming rather than implementation programming. * The design pattern also has the following essentials: less inheritance and more combinations. * The following is an example. ** @ Author qingruihappy * @ data October 31, 2018 11:26:45 * @ Note: */public class tanklog implements moveable {moveable t; Public tanklog (moveable t) {super (); this. T = T ;}@ override public void move () {system. out. println ("tank start printing logs"); T. move (); system. out. println ("tank start printing end ");}}

 

Package com. designpatterns. Al. dynamic3;/*** now let's take a look at why implementation (aggregation) is used without inheritance. * The most general statement is to implement decoupling, because the essence of the design pattern is abstract programming rather than implementation programming. * The design pattern also has the following essentials: less inheritance and more combinations. * The following is an example. ** @ Author qingruihappy * @ data October 31, 2018 11:26:45 * @ Note: */public class tanktime implements moveable {moveable t; Public tanktime (moveable t) {super (); this. T = T ;}@ override public void move () {system. out. println ("tank start printing Start Time"); long starttime = system. currenttimemillis (); T. move (); long endtime = system. currenttimemillis (); system. out. println ("tank start printing End Time"); system. out. println ("the total time spent on tank is" + (endtime-starttime ));}}
Package COM. designpatterns. al. dynamic3;/*** if we have a log printing agent, a time printing agent (or even a transaction agent, for example, I want to check whether the current person has the permission to run this method ). * What if we use inheritance? Let's let the log printing proxy inherit the target, and then let the printing time proxy inherit the time class, let the agent of the transaction inherit the class of the proxy time, and let the agent with the running permission inherit the proxy of the transaction ...... *. In addition, if we change the order, the agent that prints the log first, and then uses the time proxy to inherit the time proxy, then there will be two more proxy classes, it is a terrible thing to think about. There are too many classes. ** But we use interfaces (aggregation. Let's look at this example .) * Aggregation is a bit similar to the decorative design pattern: here we should note that these patterns actually focus more on the Use scenario, rather than the usage of specific details. * In fact, design patterns are more or less prone to polymorphism, but we note that design patterns focus more on semantics rather than syntax. * ** Now the static proxy is finished, but let's take a look at the shortcomings of this proxy. First, the proxy class and the target class must inherit the same interface (although it is a disadvantage but acceptable) * but there is another one: if there are other classes and cars that need to implement the printing time proxy, what should we do? You say that you are writing a car proxy. * If there are more than 100 agents that require time for bicycles, trains, trucks, batteries, carts, and so on, do you need more than 100 classes. ** Dynamic proxy is introduced below. * @ Author qingruihappy * @ data November 1, 2018 12:39:35 * @ Note: */public class test {public static void main (string [] ARGs) {/* tanktime M = new tanktime (new tank (); tanklog n = new tanklog (m); N. move (); */tanklog M = new tanklog (new tank (); tanktime n = new tanktime (m); N. move ();}}

 

Tank start printing LOG Tank start printing start time tank start printing end time when moving tank start printing End Time tank total time spent is 636tank start printing end
Tank start printing start time tank start printing LOG Tank start printing end tank start printing End Time tank total time spent is 270

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Design Mode (22) ----- proxy design 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.