Design Pattern learning-Decorator,-decorator

Source: Internet
Author: User

Design Pattern learning-Decorator,-decorator
What

Decorator: dynamically add some additional responsibilities to an object. The Decorator mode is more flexible than the subclass generation function.

Why

The Decorator mode is suitable for dynamically adding and deleting roles to objects. For example, in the QQ Show, we can select our own image and dynamically add clothes and decorations to make our image feel tall. Decorator applies to the following situations:
1. Add roles to a single object dynamically and transparently without affecting other objects.
2. Deal with scenarios where responsibilities can be revoked.
3. When the subclass method cannot be used for expansion.

How

In the following scenarios, FaceQ can be used to create a cartoon image. If this function is implemented, you can use the Decorator mode.
Basic dress up class

Public class Face {private String name; public Face () {} public Face (String name) {this. name = name;} public void show () {System. out. println ("start to dress up Face:" + name );}}

Face dressing

public class FaceFeature extends Face{    private Face face;    public FaceFeature(){    }    public void decorator(Face face){        this.face=face;    }    @Override    public void show() {        face.show();    }}

Round face

Public class RoundFaceFeature extends FaceFeature {@ Override public void show () {System. out. println ("add face"); super. show ();}}

Square face

Public class SquareFaceFeature extends FaceFeature {@ Override public void show () {System. out. println ("add face"); super. show ();}}

Mouth dress

public class Mouth extends Face {    private Face face;    public Mouth(){    }    public void decorator(Face face){        this.face=face;    }    @Override    public void show() {        face.show();    }}

Smiling mouth

Public class SmileMouth extends Mouth {@ Override public void show () {System. out. println ("add smile"); super. show ();}}

Shut up

Public class ClosedMouth extends Mouth {@ Override public void show () {System. out. println ("add and shut up. "); Super. show ();}}

Call the above dress-up class

    public static void main(String[] args){        Face face=new Face("test");        RoundFaceFeature roundFaceFeature=new RoundFaceFeature();        SmileMouth smileMouth=new SmileMouth();        roundFaceFeature.decorator(face);        smileMouth.decorator(roundFaceFeature);        smileMouth.show();    }

The UML diagram of the above Code is as follows:

Discuss

In the preceding example, the Face class serves as both a responsibility interface and a specific dress object. FaceFeature and Mouth are two dress classes. In java jdk, there is also a typical Decorator mode. in java io classes, Decorator modes are used in OutputStream, InputStream, Reader, and Writer, if you have time, analyze it carefully.


Decorator design mode

There are many stream-related items. I will give you an example.
InputStream is an abstract class and does not provide actual functions. However, its subclass BufferedInputStream provides the function of reading a row at a time, that is, the readline method.
 
Q: What is the use of the learning design model?

As a learning and practical design model for more than 10 years, the essence of the design model is simple and beautiful. A simple solution is to use only one line of statements to solve the problem and never use two lines. Beauty is beauty, health, nature, and how you feel when you see beautiful things. To be honest, I have no less than 10 million lines of code. Some codes are more attractive than Fan Bingbing. Some codes are more annoying than Fengjie. To put it bluntly, the design pattern is just a way for programming and designers to be simple and beautiful. In some cases, it is difficult to look at the problem of engineering with the eyes and hearts of engineering. Instead, we should try our best to reach the realm of "one by one, one by one, and one by one. When you are programming and designing software, you always think that I want to make the software system simple and beautiful, and you naturally have mastered the essence of the design model, the rest is just learning some methods and skills. Yang Lan once said that even if I do not act as the host or scan the street, I will become the most famous and best cleaners in the country.

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.