Decorative patterns of Java design patterns

Source: Internet
Author: User

Decorator Pattern Definition: the ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.
And look at his UML diagram.

When I first learned about design patterns, I wondered why it was the same with inheritance. Later know that because Java can only single inheritance, if you want to extend a lot of work can cause the expansion of the tree is larger, resulting in class management inconvenience, and sometimes not suitable for inheritance, as someone this class, and then some people wear hats, some people with earrings, some people wearing long sleeves, someone wearing short sleeves, not every kind of writing sub-class bar For example, if the hat has 10 choices, clothes 10 options, pants 10 options, if you want to inherit a total of 1000 possible costumes, you have to write 1000 sub-class, so the successor mode is compared to trial.
Here is a more interesting example of what I have seen before:
1. Work.java interface
2, Son.java is the decorator-responsible for painting
3, Mother.java Decorator-Responsible for the color
4. Father.java Decorator-Responsible for painting frame
5, Decoratortest.java with the main method of the test class,

Simulate 3 invocation Scenarios:
A, painting only;
B, painting, coloring;
C, painting, coloring, framed
1, Work.java

package decorator;publicinterface Work {  publicvoidpaint();}

2, Son.java

package decorator;publicclass Son implements Work {  publicvoidpaint() {    System.out.println("儿子用铅笔画好了一幅画。");  }}

3, Mother.java

 PackageDecorator Public  class mother implements  work {  //Decorated by  PrivateWork work; Public Mother(Work work) { This. Work = Work; }Private Mother() {} Public void Paint() {//Mother decorator does the dutySystem.out.println ("Mom is making preparations for the painting before the color. ");//The role of the decoratorWork.paint ();//Mother decorator does the dutySystem.out.println ("mother gave the picture a good color." "); }}

4, Father.java

 PackageDecorator Public  class Father implements  work {  //Decorated by  PrivateWork work; Public Father(Work work) { This. Work = Work; }Private Father() {} Public void Paint() {//Father decorator does the dutySystem.out.println ("Dad is working on the drawing frame before the preparation. ");//The role of the decoratorWork.paint ();//Father decorator does the dutySystem.out.println (dad put a picture frame in the picture. "); }}

5, Decoratortest.java

package decorator;publicclass DecoratorTest {  publicstaticvoidmain(String[] args) {    //只画铅笔画    new Son();    work.paint();       System.out.println("\n");    //除了画铅笔画,还要给画上颜色    new Mother(work);    work.paint();    System.out.println("\n");    //除了画铅笔画,给画上颜色,还要给画上画框    new Father(work);    work.paint();  }}

Output:
The son drew a picture with a pencil.

Mother is making preparations for the painting before the color.
The son drew a picture with a pencil.
Mother gave the picture a good color.

Dad is doing the work before the picture frame.
Mother is making preparations for the painting before the color.
The son drew a picture with a pencil.
Mother gave the picture a good color.
Dad put a picture frame in the picture.

See this example, do you understand how to use the decorator mode?

Copyright NOTICE: This article is the original blog article, reproduced please indicate the source

Decorative patterns of 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.