Java proxy mode

Source: Internet
Author: User
Tags stub

Today listen to a little summary of the proxy mode:

Create a new interface

 Package Com.proxy;  Public Interface Moveable {    void  Move ();}

Create a new tank class implementation moveable

 PackageCom.proxy;ImportJava.util.Random; Public classTankImplementsmoveable{@Override Public voidMove () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("Tank is running"); Try {            NewThread (). Sleep (NewRandom (). Nextint (10000)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }    }}

To record the execution time of the Move method, there are two ways we can achieve

 PackageCom.proxy;/*** Implementation of proxy through inheritance * Inheritance is not flexible if you want to add the logging function to create a new class to inherit TANK2 * Override the Move method to add a log function inside if you want to add more than n functions to create a new n multiple classes to generate * class explosion */ Public classTank2extendsTank {/*** To calculate the execution time of a subclass method*/@Override Public voidMove () {LongStart =System.currenttimemillis (); Super. Move ();//Tank2 is also a proxy call he actually calls the method of his parent class        LongEnd =System.currenttimemillis (); System.out.println ("Time:" + (end-start)); }        }

The second method impl

 PackageCom.proxy;/** One class has an implementation aggregation of another class*/ Public classTanklogproxyImplementsmoveable{Tank t;//The method of the object to be tested is the same way as the method of this class calls other people's methods//Tanklogproxy is a proxy for T     PublicTanklogproxy (Tank t) {Super();  This. T =T; } @Override Public voidMove () {System.out.println ("Tank Start");        T.move (); System.out.println ("Tank Stop"); }}

Test

 Package Com.proxy;  Public class DoMain {    publicstaticvoid  main (string[] args) {                new  Tank2 ();        M.move (); // is calling the method of his parent class. New Tanktimeproxy (new  Tank ());                  M1.move ();    }}

A more recommended way to implement the inheritance

=================== Split Line =========================

If you want to add logging functionality, we use aggregation in a way that's easier and more flexible.

Tanktimeproxy modified as follows

 PackageCom.proxy;/** One class has an implementation aggregation of another class*/ Public classTanktimeproxyImplementsmoveable{moveable t;  Publictanktimeproxy (Moveable t) {Super();  This. T =T; } @Override Public voidMove () {//TODO auto-generated Method Stub                LongStart =System.currenttimemillis ();        T.move (); LongEnd =System.currenttimemillis (); System.out.println ("Time:" + (end-start)); }}

Add Tanklogproxy

 PackageCom.proxy;/** One class has an implementation aggregation of another class*/ Public classTanklogproxyImplementsmoveable{moveable t;//moveable has the Move method to delegate all moveable content     Publictanklogproxy (Moveable t) {Super();  This. T =T; } @Override Public voidMove () {System.out.println ("Tank Start");        T.move (); System.out.println ("Tank Stop"); }}

Test

 PackageCom.proxy; Public classDoMain { Public Static voidMain (string[] args) {//Tank Tank =new Tank ();//Tanktimeproxy TTP = new Tanktimeproxy (tank);//moveable m = TTP;//M.move ();//called by the time proxy object is called inside the tank tank =new tank (), the object's Move method//if we want to move the log when adding the functionTank T =NewTank (); Tanktimeproxy Tanktimeproxy=NewTanktimeproxy (t); Tanklogproxy Tanklogproxy=NewTanklogproxy (Tanktimeproxy); Moveable M1=Tanklogproxy;            M1.move (); }}

Java proxy mode

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.