[+]
- 1. Introduction to the decoration mode Brief Introduction
- What to solve
- Analysis of Three decorative Modes
- Decorative Pattern Structure
- Source code
- Program running result
- Case study example
- Summary
1. Introduction to the decoration mode (Brief Introduction)
Dynamically add some additional responsibilities to an object.
Advantage: The decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core functions of the class from the decorative functional area.
2. What to solve)
Objects that have been developed need to expand a lot of functions to the old objects due to business needs in the future. In this case, you can dynamically add new states or actions (that is, decoration mode) to the objects) instead of using subclass static inheritance.
For example, if you just bought a car
This car does not meet your individual requirements, such as the appearance is not beautiful enough, the engine horsepower is not enough, can not meet your speed passion, so you need to decorate the car's appearance, at the same time, the engine performance needs to be improved. These operations can solve the problem through the decoration mode. The personalized car shown in is finally obtained.
3. Decorative Pattern Analysis (analysis) 1. decorative pattern structure
Component defines an object interface to dynamically add responsibilities to these objects.
Comcretecomponent specifies a specific object. You can also add responsibilities to this specific object.
The decorator abstract decoration class inherits the component object interface and extends the component class functions from the external class. However, for component, you do not need to know the existence of the decorator.
The specific decoration object of concretedecoratora is used to add responsibilities to component.
The specific decoration object of concretedecoratorb to add responsibilities to component.
2. Source Code
1. object interface component. CS |
Public abstract class component { Public abstract void operation (); } |
2. The concretecomponent. CS implementation class inherits from the component interface. |
Public class concretecomponent: Component { Public override void operation () { Console. writeline ("starting to execute a specific object ..."); } } |
3. decorator abstract class. CS inherits from the component interface. |
Public abstract class decorator: Component { Private component m_component; Public void setcomponent (Component component) { This. m_component = component; } Public override void operation () { If (m_component! = NULL) { M_component.operation (); } } } |
4. The specific decoration object concretedecoratora. CS inherits from the decorator abstract class. |
Public class concretedecoratora: decorator { Private string addedstate; Public override void operation () { Base. Operation (); Addedstate = "state attribute decoration. "; Console. writeline (addedstate ); } } |
Added the state attribute addedstate to the object interface component. |
5. The specific decoration object concretedecoratorb. CS inherits from the decorator abstract class. |
Public class concretedecoratorb: decorator { Public override void operation () { Base. Operation (); Addedbehavior (); } Private void addedbehavior () { Console. writeline ("decorated with Operation behaviors. "); } } |
Added the operation behavior addedbehavior () to the object interface component () |
5. client code |
Static void main (string [] ARGs) { Concretecomponent cc = new concretecomponent (); Concretedecoratora CDA = new concretedecoratora (); Concretedecoratorb CDB = new concretedecoratorb (); CDA. setcomponent (CC ); CDB. setcomponent (CDA ); CDB. Operation (); Console. Read (); } |
Added the operation behavior addedbehavior () to the object interface component () |
3. program running result
Iv. Case Analysis (example)
In this case, border and scrollbar are decorated for the textview component. Make textview more in line with project requirements.
Summary)
Decoration mode: dynamically add additional responsibilities to an object. These responsibilities need to be determined by the user. The decoration mode provides an "out-of-the-box" way to determine when to add a function during running. The decoration mode is more flexible than the subclass generation.
1. Introduction to the decoration mode (Brief Introduction)
Dynamically add some additional responsibilities to an object.
Advantage: The decoration function in the class can be removed from the class, which can simplify the original class. Effectively separates the core functions of the class from the decorative functional area.
2. What to solve)
Objects that have been developed need to expand a lot of functions to the old objects due to business needs in the future. In this case, you can dynamically add new states or actions (that is, decoration mode) to the objects) instead of using subclass static inheritance.
For example, if you just bought a car
This car does not meet your individual requirements, such as the appearance is not beautiful enough, the engine horsepower is not enough, can not meet your speed passion, so you need to decorate the car's appearance, at the same time, the engine performance needs to be improved. These operations can solve the problem through the decoration mode. The personalized car shown in is finally obtained.
3. Decorative Pattern Analysis (analysis) 1. decorative pattern structure
Component defines an object interface to dynamically add responsibilities to these objects.
Comcretecomponent specifies a specific object. You can also add responsibilities to this specific object.
The decorator abstract decoration class inherits the component object interface and extends the component class functions from the external class. However, for component, you do not need to know the existence of the decorator.
The specific decoration object of concretedecoratora is used to add responsibilities to component.
The specific decoration object of concretedecoratorb to add responsibilities to component.
2. Source Code
1. object interface component. CS |
Public abstract class component { Public abstract void operation (); } |
2. The concretecomponent. CS implementation class inherits from the component interface. |
Public class concretecomponent: Component { Public override void operation () { Console. writeline ("starting to execute a specific object ..."); } } |
3. decorator abstract class. CS inherits from the component interface. |
Public abstract class decorator: Component { Private component m_component; Public void setcomponent (Component component) { This. m_component = component; } Public override void operation () { If (m_component! = NULL) { M_component.operation (); } } } |
4. The specific decoration object concretedecoratora. CS inherits from the decorator abstract class. |
Public class concretedecoratora: decorator { Private string addedstate; Public override void operation () { Base. Operation (); Addedstate = "state attribute decoration. "; Console. writeline (addedstate ); } } |
Added the state attribute addedstate to the object interface component. |
5. The specific decoration object concretedecoratorb. CS inherits from the decorator abstract class. |
Public class concretedecoratorb: decorator { Public override void operation () { Base. Operation (); Addedbehavior (); } Private void addedbehavior () { Console. writeline ("decorated with Operation behaviors. "); } } |
Added the operation behavior addedbehavior () to the object interface component () |
5. client code |
Static void main (string [] ARGs) { Concretecomponent cc = new concretecomponent (); Concretedecoratora CDA = new concretedecoratora (); Concretedecoratorb CDB = new concretedecoratorb (); CDA. setcomponent (CC ); CDB. setcomponent (CDA ); CDB. Operation (); Console. Read (); } |
Added the operation behavior addedbehavior () to the object interface component () |
3. program running result
Iv. Case Analysis (example)
In this case, border and scrollbar are decorated for the textview component. Make textview more in line with project requirements.
Summary)
Decoration mode: dynamically add additional responsibilities to an object. These responsibilities need to be determined by the user. The decoration mode provides an "out-of-the-box" way to determine when to add a function during running. The decoration mode is more flexible than the subclass generation.