Decorator mode (less understood design pattern)

Source: Internet
Author: User

For the decorator mode has been not very understanding, do not understand the difference between his and the bridge mode????

One of 23 design patterns, English is called decorator pattern, also known as the decorator mode. Decorating mode is 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.

feature editing of decoration mode(1) The Decoration object and the real object have the same interface. This allows the client object to interact with the adornment object in the same way as the real object. (2) The Adornment object contains a reference to a real object (reference) (3) The Adornment object accepts all requests from the client. It forwards these requests to the real object. (4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.the difference between the decorator and the Adaptive Model editor 1. About new responsibilities: Adapters can also add new responsibilities at the time of conversion, but the primary purpose is not here. The decorator mode is mainly for the decorator to add new responsibilities. 2. About the original interface: The adapter mode is to use the new interface to invoke the original interface, the original interface to the new system is not visible or unavailable. The decorator mode uses the original interface intact, and the object that is decorated by the system is also used through the original interface. (The adorner mode that adds a new interface can be thought of as its variant--"translucent" decorator) 3. About the object of its package: The adapter is the details of the person who knows what is being adapted (that is, that class or that interface). The decorator knows only what its interface is, and whether its specific type (base class or other derived class) is known only during run time.

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,

Simulation of 3 Call cases: A, painting only;

B, painting, coloring;

C, painting, coloring, framed

=============== 1, Work.java
Package decorator;

Public interface Work {
public void paint ();
}
=============== 1 End

=============== 2, Son.java
Package decorator;

public class Son implements work {
public void paint () {
System.out.println ("The son drew a picture with a pencil. ");
}
}
=============== 2 End

=============== 3, Mother.java
Package decorator;

Public class mother implements work {

Decorated by
private work work;

Public mother (work work) {
This.work = Work;
}

Private mother () {}

public void paint () {

The duties of a mother decorator
System.out.println ("Mother is making preparations for the painting before the color.") ");

The duties of being a decorator
Work.paint ();

The duties of a mother decorator
System.out.println ("Mother gave the picture a good color.") ");
}

}
=============== 3 End

=============== 4, Father.java
Package decorator;

public class Father implements work {

Decorated by
private work work;

Public Father (work work) {
This.work = Work;
}

Private Father () {}

public void paint () {

Father Decorator does the duty
System.out.println ("Dad is working on the drawing frame before the preparation. ");

The duties of being a decorator
Work.paint ();

Father Decorator does the duty
System.out.println ("Dad put a picture frame in the picture. ");
}

}
=============== 4 End

=============== 5, Decoratortest.java
Package decorator;

public class Decoratortest {

public static void Main (string[] args) {

Draw a pencil drawing only
Work work = new Son ();
Work.paint ();
System.out.println ("\ n");

In addition to drawing pencil and painting, but also to paint the color
Work = new mother (work);
Work.paint ();
System.out.println ("\ n");

In addition to drawing pencils, painting colors, and drawing frames.
Work = new Father (work);
Work.paint ();

}
}
=============== 5 End

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.

Decorator mode (less understood 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.