Design Pattern 23-decorator)

Source: Internet
Author: User
1. Description of decoration mode 1.1 Definition

Dynamically add some additional responsibilities to an object. The decoration mode is more flexible than the subclass generation.
1.2 decoration mode highlights

Add a function to an object transparently. In other words, you need to add a function to an object, but this object cannot be known or modified.
One basic rule of object-oriented design is to try to use object combinations instead of Object Inheritance.
The decorator and the object to be decorated have the same super type.
You can wrap an object with one or more decorators.
The decorator Can add his/her own behavior before or after the behavior of the entrusted decorator to achieve a specific purpose.
Objects can be decorated at any time, so they can be dynamically decorated at runtime. You can use your favorite decorators to decorate objects in an unlimited number.
In decoration mode, the key to inheritance is to match the type of the decorator and the object to be decorated, rather than obtaining its behavior.
The decorator is generally transparent to the customer of the component unless the customer program depends on the specific type of the component. In actual projects, you can add new behaviors to the decorator as needed to achieve "Translucent.
The purpose of the adapter mode is to change the interface of an object without changing the object's performance. The purpose of the decoration mode is to keep the interface and add the responsibility of the object.
1.3 Structure Diagram and description of the decorative Mode

 
Abstract component role:An abstract interface is provided to standardize the objects preparing to receive additional responsibilities.
Concrete component role: Defines a class to receive additional responsibilities.
Decorator role: An instance that holds a component object and defines an interface consistent with the abstract Component Interface.
Concrete decorator role: Adds additional responsibilities to component objects.
1.4 decoration mode sample code

Package demo21.decorator. example3;/*** interface of the component object, dynamically add roles to these objects */public abstract class component {/*** Sample Method */public abstract void operation ();} **************************************** **************************************** * ****************** package demo21.decorator. example3;/*** specific object implementing the component object interface */public class concretecomponent extends component {public void operation () {// corresponding function processing }}******************************* **************************************** * ************************** package demo21.decorator. example3;/*** interface to maintain an interface object pointing to the component object, * define an interface consistent with the Component Interface */public abstract class decorator extends component {/*** holding the component object */protected component;/*** constructor, input component object * @ Param component object */Public decorator (Component component) {This. component = component;} public void operation () {// forward the request to the component object. You can execute some additional actions before and after forwarding component. operation ();}} **************************************** **************************************** * ***************** package demo21.decorator. example3;/*** the specific implementation object of the decorator. Add duties to the component object */public class concretedecoratora extends decorator {public concretedecoratora (Component component) {super (component );} /*** added status */private string addedstate; Public String getaddedstate () {return addedstate;} public void setaddedstate (string addedstate) {This. addedstate = addedstate;} public void operation () {// call the method of the parent class, you can execute some additional actions before and after the call // when processing here, you can use the added super. operation ();}} **************************************** **************************************** * ***************** package demo21.decorator. example3;/*** the specific implementation object of the decorator. Add responsibility */public class concretedecoratorb extends decorator {public concretedecoratorb (Component component) {super (component );} /*** responsibilities to be added */private void addedbehavior () {// implementation of duties to be added} public void operation () {// call the method of the parent class, you can execute some additional actions before and after calling super. operation (); addedbehavior ();}}
1.5 essence of the decorative Model

Dynamic combination
1.6 Advantages and Disadvantages of decoration Mode

Advantages:
More flexible than inheritance
Easier function Reuse
Simplified high-level Definition
Disadvantages:
Will produce a lot of fine-grained objects
1.7 when to choose

If you want to add responsibilities to an object in a dynamic and transparent manner, you can use the decoration mode.
If it is not suitable to use subclasses for extension, you can consider using the decoration mode.
2. Example

The most common example is the I/O data stream in Java. You can take a look at its inheritance implementation structure.
If you have just bought a bare car that does not meet your individual requirements, such as poor appearance, poor tires, and insufficient engine horsepower. Then you need to decorate the exterior, change the color, and add some cushions to the car. So here we need to use the decoration model we just learned.
The instance code is as follows, and the code comments are clear, so I will not go into details.
2.1 corresponding to component

Package demo21.decorator. example1;/*** Component Object --- car ** @ author Wang yuchao **/public abstract class car {/*** driving function */public abstract void run ();}
2.2 concretecomponent
Package demo21.decorator. example1;/*** specific car, for example, Audi ** @ author Wang yuchao **/public class audicar extends car {@ overridepublic void run () {system. out. println ("Audi is on the road... ");}}
2.3 corresponding to decorator
Package demo21.decorator. example1;/*** the modifier interface, which maintains an interface object pointing to the component object and defines an interface consistent with the component interface, this is the decoration interface */public abstract class decorator extends car {/*** holding the component object */private car;/*** constructor, input Component Object ** @ Param car * component object */Public decorator (car) {This. car = car ;}@ overridepublic void run () {// forward it to the component Component Object carthis. car. run ();}}
2.4 concretedecoratora
Package demo21.decorator. example1;/*** the specific implementation object of the decorator. Add responsibility to the component object. This is the vehicle that adds color */public class addcolorcar extends decorator {public addcolorcar (car) {super (CAR) ;}/ *** added status, change the appearance */private string face; Public void setface (string face) {This. face = face;} public void addcolor () {system. out. println ("I added color" + this. face) ;}@ overridepublic void run () {// call the method of the parent class. You can execute some additional actions before and after the call. // when processing the actions here, you can use the added super. run (); this. addcolor ();}}
2.5 concretedecoratorb
Package demo21.decorator. example1;/*** the specific implementation object of the decorator. Add responsibility to the component object. This is the car that adds the seat cushion */public class addseatcar extends decorator {public addseatcar (car) {super (CAR) ;}/ *** added status, add cushion */private string seat; Public void setseat (string seat) {This. seat = seat;} public void addseat () {system. out. println ("I added a cushion" + this. seat) ;}@ overridepublic void run () {// call the method of the parent class. You can execute some additional actions before and after the call. // when processing here, you can use the added super. run (); this. addseat ();}}
2.6 client usage
Package demo21.decorator. example1; public class client {public static void main (string [] ARGs) {// decorated object car = new audicar (); // decorator colorcar = new addcolorcar (CAR); (addcolorcar) colorcar ). setface ("red"); decorator seatcar = new addseatcar (colorcar); (addseatcar) seatcar ). setseat ("Summer cushion"); // come and run the car. run ();}}

After reading it, I still don't understand it. I suggest you take a look at the various types of Java I/O data streams and their relationships.

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.