The decorator mode of Java

Source: Internet
Author: User

Decorator pattern (adorner mode), definition:Attach Additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality. ( Add some extra responsibilities to an object dynamically. For added functionality, the decoration mode is more flexible than generating subclasses)

General view of adorners:

Four characters to explain:

1.Component Abstract Artifacts:

Component is an interface or an abstract class that defines our core objects, the most primitive objects, the highest level of abstraction, the unification of the entire adorner system, the bridge between the decorators, like the InputStream in the ii/o stream, Like OutputStream.

2.ConcreteComponent Concrete Components

Concretecomponent is the most core, the most primitive, the most basic interface or the implementation of the abstract class, you want to decorate is it, the source of the decoration, you decorate the bottom, like I/O flow, which directly with the bottom of the node flow, such as FileInputStream

3.Decorator Decorative Characters

is generally an abstract class, implement interface or abstract method, it does not necessarily have an abstract method, but in its properties must have a private variable point to component abstract component, is usually initialized with a constructor. Just like the filterinputstream in the I/O stream

4.concretedecoratora,concretedecoratorb specific decorator role, this is to be built between us the most core, the most primitive, the most basic things to decorate into something or other things, like i/ O in the Bufferedinputstream,datainputstream and so on.

A simple example is given below:

1  Packagedecorator;2Abstract artifacts3  Public InterfaceComponent4 {5     voidoperation ();6 }7The implementation of concrete abstract artifacts8  Public classConcretecomponentImplementsComponent9 {Ten  One     /** { @inheritDoc } */ A @Override -      Public voidoperation () -     { theSystem.out.println ("I am concretecomponent, is the most primitive implementation class, I am at the bottom of the"); -     } -      - } +Abstract Decorator -  Public Abstract classDecoratorImplementsComponent + { A     PrivateComponent Component; at  -     /**  -      * @paramComponent -      */  -      PublicDecorator (Component Component) -     { in          This. Component =component; -     } to      +     /** { @inheritDoc } */ - @Override the      Public voidoperation () *     { $ component.operation ();Panax Notoginseng     } - } theConcrete Adorner A +  Public classConcretedecoratoraextendsDecorator A { the  +     /**  -      * @paramComponent $      */  $      PublicConcretedecoratora (Component Component) -     { -         Super(component); the     } -     Wuyi      Public voidMethodA () the     { -System.out.println ("I am Concretedecoratora, add new Features"); Wu     } -      About     /** { @inheritDoc } */ $ @Override -      Public voidoperation () -     { - MethodA (); A         Super. Operation (); +System.out.println ("Concretedecoratora Operation execution Complete"); the     } -  $ } theConcrete Adorner B the  Public classConcretedecoratorbextendsDecorator the { the  -     /**  in      * @paramComponent the      */  the      PublicConcretedecoratorb (Component Component) About     { the         Super(component); the     } the      +      Public voidMethodB () -     { theSystem.out.println ("I am concretedecoratorb, add new Features");Bayi     } the      the     /** { @inheritDoc } */ - @Override -      Public voidoperation () the     { the MethodB (); the         Super. Operation (); theSystem.out.println ("Concretedecoratorb Operation execution Complete"); -     } the  the }93//test class94  Public classDemo the { the      Public Static voidMain (string[] args) the     {98Component Component =Newconcretecomponent (); About          -Concretedecoratorb Decoratorb =NewConcretedecoratorb (NewConcretedecoratora (component));101         102 decoratorb.operation ();103     }104}

Decorator mode for Java

Related Article

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.