Simple implementation of dynamic agent, adornment mode and inheritance in Java

Source: Internet
Author: User

Inherited

Fixed by the object being enhanced
The enhanced content is also fixed

Decorator mode

Objects that are enhanced can be toggled.
Enhanced content is fixed

There is also a more flexible approach to aspect-oriented programming (AOP), which is more flexible than decorator mode, with enhanced objects and enhanced content that can be replaced.
Package Cn.ph.proxy;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;import java.lang.reflect.proxy;//using proxy mode public class MyCar {public static void main (string[] args) {//Object java.lang.re Flect. Proxy.newproxyinstance//(ClassLoader loader, class<?>[] interfaces, Invocationhandler h) throws Illegalargument Exception//Parameter type/** * param1: tells the virtual machine which bytecode loader to load the corresponding bytecode file, fixed value * param2: Tell the virtual machine what bytecode is being created * PARAM3: Tell the virtual machine to be How the individual methods of the created bytecode file are processed * The corresponding proxy file is generated after compilation Mycar$1.class */Car car = (car) proxy.newproxyinstance (MyCar.class.getCl                Assloader (), GoogleCar.class.getInterfaces (), new Invocationhandler () {@Override                    public object invoke (object proxy, Method method, object[] args) throws Throwable {//code executed in each method                        if ("Start". Equals (Method.getname ())) {//Method.getname (). Equalsignorecase ("Start")//Ignore case System.out.println (Method.getName ());//Enhanced Start Method} Method.invoke (New Googlecar ());                return null;    }            });    Car.start ();//execute the above Invoke method Car.run (); Car.stop ();}} Reference decorator Mode class Testcar implements car {private car car;public testcar (car car) {This.car = car;} @Overridepublic void Run () {Car.run ();}    @Overridepublic void Start () {System.out.println ("view Weather");    System.out.println ("Check the Road"); Car.start ();} @Overridepublic void Stop () {car.stop ();}} Class Googlecar implements car {@Overridepublic void start () {System.out.println ("car started Up");} @Overridepublic void Run () {System.out.println ("car Ran");}    @Override public void Stop () {System.out.println ("car Stopped");    }}interface Car {void start (); void run (); void stop ();}

Dynamic agent, adornment mode, and inheritance simple implementation in Java

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.