Understanding-Adorner Mode-design mode

Source: Internet
Author: User

 PackageCom.hk.ztry;Interfacesourcable { Public voidoperation (); }  classSourceImplementssourcable { Public voidoperation () {SYSTEM.OUT.PRINTLN ("The Soure class implements the method in the original interface sourcable"); }  }classDecorator1Implementssourcable {Privatesourcable sourcable;  PublicDecorator1 (sourcable sourcable) {Super();  This. sourcable=sourcable; }             Public voidoperation () {SYSTEM.OUT.PRINTLN ("Before 1th decorator");          Sourcable.operation (); System.out.println ("After 1th adorner"); }  }  classDecorator2Implementssourcable {Privatesourcable sourcable;  PublicDecorator2 (sourcable sourcable) {Super();  This. sourcable=sourcable; }       Public voidoperation () {SYSTEM.OUT.PRINTLN ("Before 2nd decorator");           Sourcable.operation (); System.out.println ("After 2nd adorner"); }  }   Public classTestdecorator { Public Static voidMain (string[] args) {sourcable source=NewSource (); //good magic, interface =new implementation class (); //then the interface, which embodies the function of the realization class,//Interface =new Decoration class (),//then the interface, in the original implementation of the class has been decorated functionsourcable obj =NewDecorator1 (NewDecorator2 (source));      Obj.operation (); }  }

Understanding-Adorner Mode-design 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.