Tag: com inherits the ORA mode override with delegation attached
1, the understanding of the decorative mode
The ability to dynamically extend an object without having to change the original class file and use inheritance. It is by creating a wrapper object, that is, decorating to wrap the real object.
2. Decoration mode consists of 4 characters
(1) abstract component (Component) role: An abstract interface is given to standardize the objects that are ready to receive additional responsibilities.
(2) Specific component (concrete Component) Role: Defines a class that will receive additional responsibilities.
(3) decoration (Decorator) Role: Holds an instance of a component (Component) object and implements an interface consistent with the abstract component interface, extending the functionality of the Component class from an outer class, but for the Component class, There is no need to know the existence of Decorato.
(4) Specific decoration (concrete Decorator) role: Responsible for adding additional responsibilities to the component objects.
3. UML class diagram of decoration mode
4, the characteristics of decorative mode
(1) The Decoration object and the real object have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
(2) The Adornment object contains a reference to a real object.
(3) The Adornment object accepts all requests from the client. It forwards these requests to the real object.
(4) Decorative objects can add additional functionality before or after forwarding these requests. This ensures that additional functionality can be added externally without modifying the structure of a given object at run time. In object-oriented design, the extension of functionality to a given class is usually achieved through inheritance.
5. Applicability
(1) You need to extend the functionality of a class or add additional responsibilities to a class.
(2) The need to dynamically add functionality to an object, these functions can be re-dynamic revocation.
(3) It is necessary to increase the number of functions generated by the permutation of some basic functions, thus making the inheritance relationship impractical.
(4) When a method of generating subclasses cannot be used to augment. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.
6. Code implementation
package com.ixunm.decorate; /** abstract component (Component) role: An abstract interface is given to standardize the objects that are ready to receive additional responsibilities */ public interface Component { // Simple action method public void Sampleoperation ();}
Package com.ixunm.decorate; /** */Publicclassimplements component{ @Override Public void sampleoperation () { // write related business code System.out.println ("Specific object action");} }
Packagecom.ixunm.decorate;/*** Decoration (Decorator) Role: Holds an instance of a component (Component) object and defines an interface that is consistent with the abstract component interface. */ Public classDecoratorImplementscomponent{PrivateComponent Component; /*** Constructor Method initializes abstract artifact Object *@paramComponent*/ PublicDecorator (Component Component) { This. Component =component; } @Override Public voidsampleoperation () {//delegating to Artifact object handling if(Component! =NULL) {component.sampleoperation (); } }}
Packagecom.ixunm.decorate;/*** Specific decorative character ConcreteDecorator01: Unique function of this class * specific decoration (concretedecorator) role: Responsible for the Component object "affixed" additional responsibilities. */ Public classConcreteDecorator01extendsdecorator{/*** Construction Method initializes abstract artifact Object * *@paramComponent*/ PublicConcreteDecorator01 (Component Component) {Super(component); } @Override Public voidsampleoperation () {Super. Sampleoperation ();//perform the Sampleoperation () method of the original component//unique features of this class: write related business codeSYSTEM.OUT.PRINTLN ("Operation of the specific decorative object ConcreteDecorator01"); }}
Packagecom.ixunm.decorate;/*** Specific decorative character ConcreteDecorator02: Unique function of this class * specific decoration (concretedecorator) role: Responsible for the Component object "affixed" additional responsibilities. */ Public classConcreteDecorator02extendsdecorator{/*** Construction Method initializes abstract artifact Object * *@paramComponent*/ PublicConcreteDecorator02 (Component Component) {Super(component); } @Override Public voidsampleoperation () {Super. Sampleoperation ();//perform the Sampleoperation () method of the original component//unique features of this class: write related business methods, execute related businessmysampleoperation (); } /*** Unique features of this class*/ Private voidmysampleoperation () {System.out.println ("Operation of the specific decorative object ConcreteDecorator02"); }}
package com.ixunm.decorate; public class Demo { public static void main (string[] args) {concretecomponent concretecomponent = new concretecomponent (); ConcreteDecorator01 Decorator01 = new ConcreteDecorator01 (concretecomponent); ConcreteDecorator02 decorator02 = new ConcreteDecorator02 (concretecomponent); Decorator01.sampleoperation (); Decorator02.sampleoperation (); }}
7. Testing
8. Expansion
The design pattern of Io stream in Java, using the decorative mode
Www.cnblogs.com/wxgblogs/p/5649933.html
The decorative mode of Java