Decorative Mode of design pattern

Source: Internet
Author: User

Decoration mode

Dynamically adds some additional responsibilities to an object, and the decoration mode is more flexible than generating subclasses for added functionality

Person class:

 Package Com.hml.decorator;  Public class Person {    private  String name;      Public String GetName () {        return  name;    }      Public void setName (String name) {        this. Name = name;    }       Public void Show () {    }}

Clothing category

 package   Com.hml.decorator;  public  class  Finery extends   person { protected   person component;  public  void   decorate (person component) {  component;  public  void   Show () { if  (Component! = null  ) {Component.show (); }    }}

T sleeve type

 Package Com.hml.decorator;  Public class extends Finery {    publicvoid  Show () {        System.out.println ("T-sleeve");}    }

Large underwear Category:

 Package Com.hml.decorator;  Public class extends Finery {    publicvoid  Show () {        System.out.println ("big underwear");}    }

Next, we can create an object, then decorate it with T-sleeves and decorate it with a large underwear.

Class Diagram:

Features of decoration mode

Decorating mode is a way to add more functionality to an existing feature dynamically, removing the decorative function from the class, which simplifies the original class.

Decorative Mode of design pattern

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.