First, what is the decorative mode
The behavior of the class is increased by the association mechanism, and the extension of its behaviors is determined by the modified object;
Ii. Supplementary Notes
Similar to inheritance, the difference is that inheritance extends the parent class during compilation, while the adorner mode dynamically expands the original object during run time;
In other words, inheritance is the extension of the class, and the adornment mode is the extension of the object;
Third, the role
Abstract artifacts
Concrete Components
Abstract Decoration Class
Concrete Decoration Class
Description: The common parent class of concrete component, abstract decoration class and concrete decoration class is abstract component, the concrete adornment class inherits abstract adornment class and decorates concrete component during running;
Iv. examples
Example Description:
painter interface Painter, for abstract components, there are two ways to obtain the painter's descriptive information and painting;
Paintbeginner implements painter interface for concrete components;
Painterdecorator implements the painter interface, which is an abstract decoration class, which is internally associated with an painter object, which is obtained through a constructor function;
Hillpainterdecorator, Riverpainterdecorator, treepainterdecorator for the specific decoration category, indicating that the decorated painter can draw hill, river, Tree;
Class Diagram:
Code implementation:
Painter.java
Package Com.pichen.dp.decorator; Public Interface Painter { publicabstract String getdescription (); Public Abstract String Painting (); }
View Code
Paintbeginner.java
Package Com.pichen.dp.decorator; Public class Implements painter{ @Override public String getdescription () { return "" ; } @Override public String Painting () { /** / return ""; }}
View Code
Painterdecorator.java
package Com.pichen.dp.decorator; public abstract class Painterdecorator implements painter{ Span style= "color: #0000ff;" >private Painter Decoratedpainter; public Painterdecorator (Painter Decoratedpainter) { this . Decoratedpainter = Decoratedpainter; public Painter Getpainter () {return this .decoratedpainter; }}
View Code
Hillpainterdecorator.java
PackageCom.pichen.dp.decorator; Public classHillpainterdecoratorextendspainterdecorator{ Publichillpainterdecorator (Painter paper) {Super(paper); } @Override PublicString getdescription () {return This. Getpainter (). GetDescription () + "Can Paint Hill,"; } @Override PublicString Painting () {/*painting The Hill*/ return This. Getpainter (). Painting () +Paintinghill (); } PublicString Paintinghill () {return"Hill,"; }}View Code
Riverpainterdecorator.java
PackageCom.pichen.dp.decorator; Public classRiverpainterdecoratorextendspainterdecorator{ Publicriverpainterdecorator (Painter paper) {Super(paper); } @Override PublicString getdescription () {return This. Getpainter (). GetDescription () + "Can paint River,"; } @Override PublicString Painting () {/*painting the river*/ return This. Getpainter (). Painting () +Paintingriver (); } PublicString Paintingriver () {return"River,"; }}View Code
Treepainterdecorator.java
PackageCom.pichen.dp.decorator; Public classTreepainterdecoratorextendspainterdecorator{ Publictreepainterdecorator (Painter paper) {Super(paper); } @Override PublicString getdescription () {return This. Getpainter (). GetDescription () + "can paint Tree,"; } @Override PublicString Painting () {/*Painting the tree*/ return This. Getpainter (). Painting () +Paintingtree (); } PublicString Paintingtree () {return"Tree,"; }}View Code
Main.java
PackageCom.pichen.dp.decorator; Public classMain { Public Static voidMain (string[] args) {Painter p0=NewPaintbeginner (); System.out.println ("Painter Description:" +p0.getdescription ()); System.out.println ("Painting:" + p0.painting () + "\ n"); Painter P1=NewHillpainterdecorator (NewRiverpainterdecorator (NewTreepainterdecorator (NewPaintbeginner ()))); System.out.println ("Painter Description:" +p1.getdescription ()); System.out.println ("Painting:" + p1.painting () + "\ n"); Painter P2=NewRiverpainterdecorator (NewHillpainterdecorator (NewPaintbeginner ())); System.out.println ("Painter Description:" +p2.getdescription ()); System.out.println ("Painting:" + p2.painting () + "\ n"); }}
The result of the execution is as follows, the object of the Paintbeginner class is not decorated, no behavior, and after being decorated by the adorner, its behavior can be dynamically expanded:
V. JAVA IO streaming and decorating mode
Here's a simple example of reader, BufferedReader, FileReader, the following code:
New BufferedReader ( new FileReader ( "test.txt")); Br.readline ();
Description
Among them, BufferedReader and BufferedReader have a common abstract parent class Reader,reader as abstract component;
The new FileReader ("Test.txt") is a concrete member, and the object is modified during operation;
BufferedReader is a specific modification class, which modifies concrete components during operation;
After decoration, the new behavior of the modified object is to have the ReadLine method;
PS: Look at the source code, did not find bufferedreader corresponding to the abstract decoration class, the individual feel that there is no abstract decoration class, decorative mode can also work normally, abstract components (Reader) can be related to the specific modified class;
In addition, the specific modifier class can also be used as a base class, inherited by other classes, the inherited class is also a concrete modification class, such as LineNumberReader is inherited BufferedReader;
Therefore, the above statement can also be written like this (PS: Just for example, in fact, there is no need to decorate with bufferedreader, directly linenumberreader decoration can be):
New LineNumberReader (New BufferedReader ( new FileReader ( new File ("Test.txt"))) ; Br.readline ();
Links
Adorner mode ★★★☆☆
Builder or Builder mode ★★☆☆☆
Abstract Factory mode ★★★★★
Factory method Mode ★★★★★
Simple Factory mode ★★★★☆
Decorator mode and Java IO stream example ★★★☆☆