A simple proxy mode for Java design patterns

Source: Internet
Author: User

Here's a simple code, plus some personal understanding of the purpose of this code: to output additional information before and at the end of an existing output. Output such as the following:
    //正常的输出结果:    System.out.println("run ......");    //要实现的样式:    xxxxxxxxx;        //(其中xxxx表示的是为任意的输出)    System.out.println("run .......");    xxxxxxxxx;        //(其中xxxx同正常输出之前的输出)
There are two ways to implement the method described above: Method one: (output the above style through inheritance)
    //使用继承实现上述的要求    //定义一个在LogProxy    interface Moveable{    public void move();}class Demo implements Moveable{    public void move(){        System.out.println("run........");    }}class LogProxy extends Demo {    public void move(){        System.out.println("Car Log start ........");        super.move();        System.out.println("Car Log end ..........");    }}class Test{    public static void main(String[] args){        LogProxy lp  = new LogProxy ();                lp.move();    }}
Using the above code can achieve the desired results, but the disadvantage is that you want to nest multiple similar output results, you need to inherit multiple classes, it is inconvenient to implement. Method Two: (by means of the agent to achieve the requirements) before writing the code needs to understand a concept is aggregation: I look at the information and video simple understanding of the meaning of the aggregation is to include in one class in addition to the other class.
Import java.util.random;interface moveable{public void Move ();                Class Tank implements moveable{public void Move () {System.out.println ("Run ...");                try{Thread.Sleep (New Random (). Nextint (10000));        The meaning here is to stop the thread for a period of time}catch (Exception e) {System.out.println (e);        }}}class Tanktimeproxy implements moveable{moveable T;        Public tanktimeproxy (moveable m) {super (); This.    t = m;        } public void Move () {System.out.println ("Time Proxy begin ...");        T.move ();    System.out.println ("Time Proxy end ...");        }}class Tanklogproxy implements moveable{moveable T;        Public tanklogproxy (moveable m) {super ();    THIS.T = m;                } public void Move () {System.out.println ("Log Proxy begin ...");                T.move ();    System.out.println ("Log Proxy end ..."); }}class demo{public static void Main (string[] args){Moveable m = new Tank ();                Tanklogproxy TLP = new Tanklogproxy (m);                Tanktimeproxy TTP = new Tanktimeproxy (TLP);    Ttp.move (); One condition for}}//to use the proxy implementation of the requirements of the beginning is that the class to be used needs to implement a unified interface, and the interface implemented in the code above is moveable.

Simple proxy mode for Java design patterns

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.