Decoration Mode
Dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.
The specific decorator and abstract decoration classes both inherit from the abstract decorator class and inherit from the type rather than behavior. Behavior comes from the decorator and basic components, or a combination with other decorator.
The decorator is generally created in a mode similar to a factory or generator.
Example
Abstract inherited class: Beverage. java
[Java]
Package com. designpattern. decorator;
// Decorator abstract class
Public abstract class Beverage {
String description = "Unknown Beverage ";
Public String getDescription (){
Return description;
}
Public abstract double cost ();
}
Specific decoration classes: HouseBlend. java, DarkRoast. java, Decat. java, Espresso. java, only one such class is provided.
[Java]
Package com. designpattern. decorator;
// The specific class of the decorator
Public class HouseBlend extends Beverage {
Public HouseBlend (){
Description = "House Blend Coffee ";
}
@ Override
Public double cost (){
// TODO Auto-generated method stub
Return 0.89;
}
}
Modifier abstract class: CondimentDecorator. java
[Java]
Package com. designpattern. decorator;
// Modifier abstract class
Public abstract class CondimentDecorator extends Beverage {
Public abstract String getDescription ();
}
Specific modifier class: Mocha. java, Soy. java, Milk. java, and Whip. java. Only one such modifier is provided.
[Java]
Package com. designpattern. decorator;
// The type of the Modifier
Public class Mocha extends CondimentDecorator {
Beverage beverage;
Public Mocha (Beverage beverage ){
This. beverage = beverage;
}
@ Override
Public String getDescription (){
// TODO Auto-generated method stub
Return beverage. getDescription () + ", Mocha ";
}
@ Override
Public double cost (){
// TODO Auto-generated method stub
Return 0.20 + beverage. cost ();
}
}
Test class: Test. java
[Java]
Package com. designpattern. decorator;
Public class Test {
Public static void main (String [] args ){
// No seasoning is required.
Beverage beverage1 = new Espresso ();
System. out. println (beverage1.getDescription () + "$" + beverage1.cost ());
// Three decorators are used to decorate DarkRoast.
Beverage beverage2 = new DarkRoast ();
Beverage2 = new Mocha (beverage2 );
Beverage2 = new Soy (beverage2 );
Beverage2 = new Whip (beverage2 );
System. out. println (beverage2.getDescription () + "$" + beverage2.cost ());
// Two decorators are used to decorate HouseBlend.
Beverage beverage3 = new HouseBlend ();
Beverage3 = new Mocha (beverage3 );
Beverage3 = new Soy (beverage3 );
System. out. println (beverage3.getDescription () + "$" + beverage3.cost ());
}
}
Summary
1) inheritance is one of the extensions, but it is not necessarily the best way to achieve elastic design.
2) in the design, the behavior should be allowed to be extended without modifying the existing code.
3) combination and delegation can be used to dynamically add new behaviors during runtime.
4) Apart from inheritance, the modifier mode also allows us to expand our behavior.
5) The decorator Mode means a group of decorator classes, which are used to wrap specific components.
6) The modifier class reflects the component type to be decorated (in fact, they have the same type and are implemented through interfaces or inheritance ).
7) the decorator Can add his or her own behavior before/or after the behavior of the decorator, and even replace the behavior of the decorator for a specific purpose.
8) You can package a component with countless decorators.
9) The decorator is generally transparent to the customer of the component, unless the customer program depends on the specific type of the component.
10) the decorator will lead to many small objects in the design. Excessive use will complicate the program.