Hello... let me talk about the modifier pattern at night ..
As the name suggests, it is a decoration. for example, wearing clothes... you wear a windbreaker and a sweater. Use these two things to decorate yourself...
Let's start with a theory.
The decoration mode is also called the packaging mode. You can extend the object function in a transparent way to the client, that is, the client does not think that the object is different before and after decoration.
Roles in the decoration mode include:
Abstract component role:An abstract interface is provided to standardize the objects preparing to receive additional responsibilities.
Specific Component roles:Defines a class to receive additional responsibilities.
Decoration role:An instance that holds a Component object and defines an interface consistent with the abstract Component Interface.
Specific decorative roles:Attaches the attached responsibility to the component object.
See the UMl diagram.
Okay. Let's take a look at the example we just put on clothes.
See the UML diagram.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + v7TPwrT6wus8L3A + fill "brush: java;"> package edu. fjnu. cs. hwb. wrapperpattern. test; public interface Wear {void say ();}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class People implements Wear {public void say () {System. out. println ("I'm wearing short sleeves ");}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class People implements Wear {public void say () {System. out. println ("I'm wearing short sleeves ");}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class WindCoat extends Clothes {Wear wear; public WindCoat (Wear wear) {this. wear = wear;} public void say () {System. out. println ("I'm wearing a windbreaker"); wear. say ();}}
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class WoollenSweater extends Clothes {Wear wear; public WoollenSweater (Wear wear) {this. wear = wear;} public void say () {System. out. println ("I'm wearing a sweater"); wear. say ();}}
Check the client usage
Package edu. fjnu. cs. hwb. wrapperpattern. test; public class Client {public static void main (String [] args) {Wear p = new People (); p. say (); System. out. println (); p = new WoollenSweater (p); p. say (); System. out. println (); p = new WindCoat (p); p. say (); System. out. println (); Wear wear = new WindCoat (new WoollenSweater (new People (); // the final result is the same as the above. // It can be seen that the final decoration is People. He was wearing short sleeves, but after the decoration, he put on a sweater and a windbreaker! Wear. say ();}}
Result
If there is an error, I hope to point it out!