Decorative mode (Decorator pattern) allows you to add new functionality to an existing object without changing its structure. This type of design pattern belongs to the structural pattern, which is a wrapper as an existing class.
This pattern creates an adornment class that wraps the original class and provides additional functionality, while preserving the integrity of the class method signature.
In the big talk design mode, geoscience teacher gives the definition, decoration mode: Dynamically to an object to add some additional responsibilities, in terms of adding functionality, the decoration mode is more flexible than the generation of sub-classes.
The decorative pattern structure is shown below:
Key code:
1. The Component class acts as an abstract role and should not be implemented specifically.
2, the modification class references and inherits the Component class, the concrete extension class overrides the parent class method.
We demonstrate the use of decorative patterns using the following example. Among them, we will decorate a shape with different colors without changing the shape class.
The class diagram is as follows:
//abstract shape interface, you can add responsibilities to these objects Package Com.dfcdemo; public interface Shape {public abstract void draw ();}
//specific Shape objects, you can add responsibilities to these objects package Com.dfcdemo; public class circle implements shape { @Override public void draw () {System.out.println ( "shape:circle." ); }}
//具体形状对象,可以给这些对象添加职责package com.dfcDemo;publicclass Rectangle implements Shape{ @Override publicvoiddraw() { System.out.println("shape:rectangle."); }}
PackageCom.dfcdemo;/** * Shape Interface Abstract Decoration class * @author LMB * * * * Public Abstract class shapedecorator implements Shape{ PublicShape Decoratedshape;a decorated object is to be passed to the construction method in the//shapedecorator class Public Shapedecorator(Shape Decoratedshape) { This. Decoratedshape = Decoratedshape; }@Override Public void Draw() {Decoratedshape.draw (); }}
//shape interface of the specific decoration class, inherited from the abstract decorative class Shapedecorator PackageCom.dfcdemo; Public class redshapedecorator extends shapedecorator{ PublicRedshapedecorator Decoratedshape;//construct method to pass in a decorated Shape object Public Redshapedecorator(Shape Decoratedshape) {Super(Decoratedshape); }@Override Public void Draw() {Decoratedshape.draw ();//Draw out a graphicSetredborder (Decoratedshape);//Add a red border to this graphic}//shape Interface Abstract Ornament class subclass Redshapedecorator a decorative shape implementation class decoration method Private void Setredborder(Shape Decoratedshape) {System.out.println ("Border color:red"); }}
//Test classPackage Com.dfcdemo; Public classTestdecorator { Public Static void Main(string[] args) {Shape Circle =NewCircle (); Shape redcircle =NewRedshapedecorator (NewCircle ()); Shape Redrectangle =NewRedshapedecorator (NewRectangle ()); System. out. println ("Circle with normal border"); Circle.draw ();//1, drawing graphicsSystem. out. println ("\ncircle of Red border"); Redcircle.draw ();///1, Draw graphics 2, add red borderSystem. out. println ("\nrectangle of Red border"); Redrectangle.draw ();///1, Draw graphics 2, add red border}}
Operation Result:
Circle with normal bordershape:circle.CircleRedRectangleRed
In this instance, the adornment mode uses SetBorder () to wrap the object. In this way, the implementation of each adornment object is separated from the use of the object, and each decorator only cares about its own function, and does not need to be concerned about how it is added to the object chain.
Decorating mode is a way to dynamically add more functionality to an existing feature. But when are we going to use them?
When the system needs new functionality, it is adding new code to the old class. These new additions often adorn the core duties and behaviors of the original class, such as the use of a red border to decorate circle and rectangle, but the big problem is that they add new fields, new methods, and new logic to the main class, increasing the complexity of the main class, just like the first shape class , and these newly added things are just the need to meet some special behavior that will only be performed under certain circumstances. The decorating mode provides a very good solution, which puts each function to be decorated in a separate class and lets the class wrap the object it wants, so that when special behavior needs to be performed, the client code can wrap the object at run time according to the selected, sequential use of the adornment function.
So we can sum up:
the advantages of decoration mode : The decoration function of the class is removed from the class, so that the original class can be simplified, so that the core functions of the class are separated from the decorative function area, and the duplicated decoration logic in the related class can be removed; in short, the decoration class and the decorated class can be independently developed without coupling with each other. , decoration mode is an alternative mode of inheritance, and adornment mode can dynamically extend the function of an implementation class.
The disadvantage of decorative mode : Multilayer decoration is more complex.
usage Scenario : 1. Extend the functionality of a class. 2, dynamic increase function, dynamic revocation.
caveats: can replace inheritance
Big talk design mode-decorative mode