java-Dynamic Agent-Preliminary

Source: Internet
Author: User

I. Overview

1. Objective: To do time agent and log agent in Tank's Move () method (you can imagine adding a lot of agent processing later), and the order between agents can be changed

2. Ideas:

(1) Aggregation: Because the proxy class and the proxy class have implemented the movable interface, the proxy class aggregates the proxy class, so it can be flexible, the specific look at the code

(2) Inheritance: Inheritance is not flexible enough to see the code

Second, the Code

1.movable.java

2.tank.java

3.tanktimeproxy.java

4.tanklogproxy.java

5.tank2time.java

6.tank3log.java

7.client.java

1.movable.java

1  Public Interface Movable {2      Public void move (); 3 }

2.tank.java

1 ImportJava.util.Random;2 3  Public classTankImplementsMovable {4 5 @Override6      Public voidMove () {7System.out.println ("Tank moving ...");8         Try {9Thread.Sleep (NewRandom (). Nextint (5000));Ten}Catch(interruptedexception e) { One e.printstacktrace (); A         } -     } -  the}

3.tanktimeproxy.java

1  Public classTanktimeproxyImplementsMovable {2 3 movable m;4     5      Publictanktimeproxy (movable m) {6          This. m =m;7     }8 9 @OverrideTen      Public voidMove () { OneSystem.out.println ("Time Proxy start .....")); A         LongStart =System.currenttimemillis (); - M.move (); -         LongEnd =System.currenttimemillis (); theSystem.out.println ("Spend time:" + (End-start)); -System.out.println ("Time Proxy end .....")); -     } -  +}

4.tanklogproxy.java

1  Public classTanklogproxyImplementsMovable {2 3 movable m;4     5      Publictanklogproxy (movable m) {6          This. m =m;7     }8 9 @OverrideTen      Public voidMove () { OneSystem.out.println ("Log Proxy start ....")); A M.move (); -System.out.println ("Log Proxy end ....")); -     } the  -}

5.tank2time.java

1  Public classTank2timeextendsTank {2 3      Public voidMove () {4System.out.println ("Tank2 time start ...."));5         LongStart =System.currenttimemillis ();6         Super. Move ();7         LongEnd =System.currenttimemillis ();8System.out.println ("Spend time:" + (End-start));9System.out.println ("Tank2 time End ...."));Ten     } One}

6.tank3log.java

1  Public class extends tank2time {23      Public void Move () {4         System.out.println ("Tank3log start .....  "); 5         Super . Move (); 6         System.out.println ("Tank3log  end ....."); 7     }8 }

7.client.java

1  Public classClient {2 3 @Test4      Public voidTestproxy () {5         6Tank T =NewTank ();7 movable m;8         9         //one, the way of aggregation (more flexible, because the interface is implemented)Ten         //1.1 Aggregation mode of the agent, the first log agent, after the time agent OneTanktimeproxy TTP1 =NewTanktimeproxy (t); ATanklogproxy TLP1 =NewTanklogproxy (TTP1); -          -m =TLP1; the M.move (); -          -System.out.println ("\n============================== Divider line ==========================\n"); -          +         //1.2 Aggregation mode of the agent, the first time agent, after the log agent (can be flexibly switched order) -Tanklogproxy TLP2 =NewTanklogproxy (t); +Tanktimeproxy TTP2 =NewTanktimeproxy (TLP2); A          atm =TTP2; - M.move (); -          -System.out.println ("\n============================== Divider line ==========================\n"); -          -         //Ii. the way of inheritance in         //2.1 Agent Time -Tank2time t2 =Newtank2time (); to T2.move (); +          -System.out.println ("\n============================== Divider line ==========================\n"); the          *         //2.2 Agent Log, after time, unable to switch flexibly $tank3log t3 =NewTank3log ();Panax Notoginseng T3.move (); -          the          +          A     } the}

Iii. Results of operation

Iv. Summary

All functions requiring flexibility and versatility, most of which are implemented by interface polymorphism

java-Dynamic Agent-Preliminary

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.