Decorator mode for Java design patterns

Source: Internet
Author: User

Application Scenarios for Adorner mode:
1, need to extend the functionality of a class.
2, dynamic for an object to add functionality, but also can be dynamically revoked. (Inheritance cannot do this, the inherited functionality is static and cannot be dynamically deleted.) )

Source interface:
1  Public Interface sourceable {23     void method (); 4 }

SOURCE class:

1  Public class Implements sourceable {2    @Override3public     void  Method () {4         System.out.println ("Source.method () ..."); 5     }6 }

Adorner class:

1  Public classDecoratorImplementssourceable {2 3     Privatesourceable Source;4      PublicDecorator (sourceable source) {5          This. Source =Source;6     }7 8 @Override9      Public voidmethod () {TenSystem.out.println ("Decorator decorate 1"); One Source.method (); ASystem.out.println ("Decorator decorate 2"); -     } -  the      Public Static voidMain (string[] args) { -sourceable Source =NewSource (); -sourceable sourceable =NewDecorator (source); - Sourceable.method (); +     } -}

Decorator 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.