Design Pattern-seven decorators Pattern

Source: Internet
Author: User

Hello... let me talk about the modifier pattern at night ..

As the name suggests, it is a decoration. for example, wearing clothes... you wear a windbreaker and a sweater. Use these two things to decorate yourself...

Let's start with a theory.

The decoration mode is also called the packaging mode. You can extend the object function in a transparent way to the client, that is, the client does not think that the object is different before and after decoration.

Roles in the decoration mode include:

Abstract component role:An abstract interface is provided to standardize the objects preparing to receive additional responsibilities.

Specific Component roles:Defines a class to receive additional responsibilities.

Decoration role:An instance that holds a Component object and defines an interface consistent with the abstract Component Interface.

Specific decorative roles:Attaches the attached responsibility to the component object.

See the UMl diagram.



Okay. Let's take a look at the example we just put on clothes.

See the UML diagram.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + v7TPwrT6wus8L3A + fill "brush: java;"> package edu. fjnu. cs. hwb. wrapperpattern. test; public interface Wear {void say ();}

Package edu. fjnu. cs. hwb. wrapperpattern. test; public class People implements Wear {public void say () {System. out. println ("I'm wearing short sleeves ");}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class People implements Wear {public void say () {System. out. println ("I'm wearing short sleeves ");}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class WindCoat extends Clothes {Wear wear; public WindCoat (Wear wear) {this. wear = wear;} public void say () {System. out. println ("I'm wearing a windbreaker"); wear. say ();}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class WoollenSweater extends Clothes {Wear wear; public WoollenSweater (Wear wear) {this. wear = wear;} public void say () {System. out. println ("I'm wearing a sweater"); wear. say ();}}
Check the client usage

Package edu. fjnu. cs. hwb. wrapperpattern. test; public class Client {public static void main (String [] args) {Wear p = new People (); p. say (); System. out. println (); p = new WoollenSweater (p); p. say (); System. out. println (); p = new WindCoat (p); p. say (); System. out. println (); Wear wear = new WindCoat (new WoollenSweater (new People (); // the final result is the same as the above. // It can be seen that the final decoration is People. He was wearing short sleeves, but after the decoration, he put on a sweater and a windbreaker! Wear. say ();}}
Result




If there is an error, I hope to point it out!


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.