Decoration mode
Dynamically adds some additional responsibilities to an object, and the decoration mode is more flexible than generating subclasses for added functionality
Person class:
Package Com.hml.decorator; Public class Person { private String name; Public String GetName () { return name; } Public void setName (String name) { this. Name = name; } Public void Show () { }}
Clothing category
package Com.hml.decorator; public class Finery extends person { protected person component; public void decorate (person component) { component; public void Show () { if (Component! = null ) {Component.show (); } }}
T sleeve type
Package Com.hml.decorator; Public class extends Finery { publicvoid Show () { System.out.println ("T-sleeve");} }
Large underwear Category:
Package Com.hml.decorator; Public class extends Finery { publicvoid Show () { System.out.println ("big underwear");} }
Next, we can create an object, then decorate it with T-sleeves and decorate it with a large underwear.
Class Diagram:
Features of decoration mode
Decorating mode is a way to add more functionality to an existing feature dynamically, removing the decorative function from the class, which simplifies the original class.
Decorative Mode of design pattern