"Design pattern Refinement" Learning notes (11)------Decorator (decoration) mode

Source: Internet
Author: User

"Design pattern Refinement" Learning notes (11)------Decorator (decoration) mode

GoF : Dynamically adding some additional responsibilities to an object. Decorator (decorative) mode is more flexible than generating subclasses in terms of adding functionality.

In the process of system development, we want to add some functionality to an object rather than to an entire class. For example: We have to do a door, now in order to be able to make the door to the beautiful and safe need to add locks and handles, but also to the door with windows and carved. Here we call these things to add to the properties of the door.

Using inheritance mechanisms is an effective way to add functionality, and properties inherited from other classes can be used by instances of multiple subclasses. But this approach is not flexible, because if our doors are fine and the price is high, some customers may not need such a door. This allows us to flexibly add the properties of the door.

A more flexible approach is to use the decorator mode. The effect of the decorator mode is that we can create an object "chain" that starts with a decorator object-the object responsible for the new function-and ends with the original object.

The decorator pattern helps us break down the problem into two parts:

• How to implement objects that provide new functionality.

L How to organize objects for each particular situation.

A real example is the Java I/O class is widely used in the decorator model, we are interested to be able to study. I haven't studied it until now.

The decoration mode includes the following roles:

Abstract widget (Component): gives an abstract interface to specification for objects that receive additional responsibility.

L Concrete Widget (concrete Component): Defines a class that will receive additional responsibility.

L Decoration (decorator): Holds an instance of a Component object and defines an interface that is consistent with an abstract component interface.

L Specific Decoration (concrete decorator): Responsible for "affixed" to the Component object "attached" responsibility.

The following is a UML diagram of the decorator schema:

Here is the case code:

Package decorator;

/*

* Define an abstract class to derive a specific gate (Door) and a decorative abstract class (decorator)

*/

Public abstract class Component

{

public abstract void sampleoperation ();

}//end Abstract class Component

Package decorator;

/*

* A specific door

*/

public class Door extends Component

{

public void Sampleoperation ()

{

System.out.println ("Build a door!");

}//end sampleoperation ()

}//end class Door

Package decorator;

/*

* A decorative abstract class that derives a lock and a handle to decorate the door

*/

Public abstract class decorator extends Component

{

Private Component Component;

Public decorator (Component Component)

{

This.component = component;

}//end Decorator

public void Sampleoperation ()

{

This.component.sampleOperation ();

}//end sampleoperation ()

}//end Abctract class Decorator

Package decorator;

/*

* A handle to decorate the door

*/

public class Knob extends decorator

{

Public Knob (Component Component)

{

Super (component);

}//end ConcreteDecorator2 (...)

public void Sampleoperation ()

{

Super.sampleoperation ();

System.out.println ("Add an knob (handle)");

}//end sampleoperation ()

}//end CLASS Knob

Package decorator;

/*

* Door locks used to decorate doors

*/

public class Lock extends decorator

{

Public Lock (Component Component)

{

Super (component);

}//end ConcreteDecorator1 (...)

public void Sampleoperation ()

{

Super.sampleoperation ();

System.out.println ("Add a Lock (lock)");

}//end sampleoperation ()

}//end class Lock

Package decorator;

public class Decoratorpattern

{

Private Component door = new Door ();//Create a door (downward transition)

Private Component lock = new Lock (door);//Add a lock (downward transition)

Private Component knob = new Knob (lock);//Add a handle (downward transition)

public void Showdecorator ()

{

Door.sampleoperation ();

Lock.sampleoperation ();

Knob.sampleoperation ();

}//end Showdecorator ()

public static void Main (string[] args)

{

System.out.println ("Decorator pattern!/n");

Decoratorpattern DP = new Decoratorpattern ();

Dp.showdecorator ();

}//end Main (...)

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.