Adorner mode
Adorner 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 dynamically adds additional functionality to an object while preserving the integrity of the class method signature.
We demonstrate the use of the adorner pattern using the following example. Simulate a person who wants to eat, to find a restaurant, to enjoy food, to end the process of eating
Code Show:
First, create a modified interface Eat
Package decorator; // Eating Interface Public Interface Eat { // meal method void Eat ();}
Create a modified person and have a status of their own
Package decorator; Public class Implements Eat { @Override publicvoid Eat () { System.out.println ("====== I'm hungry ====== ");} }
Abstract class for creating a decorator
Package decorator; Public Abstract class Implements Eat { private Eat Eat; Public likeeat (Eat Eat) { this.eat=Eat; } @Override publicvoid eat () { eat.eat (); }}
Create an extended class of five decorated classes (that is, extended adornments), each with its own specific state, enriching the modified class
Public classFindinmapextendsLikeeat { PublicFindinmap (Eat Eat) {Super(EAT); } Public voidUserMap () {System.out.println ("Open the map for food"); } @Override Public voideat () {Super. Eat (); UserMap (); }} Public classGotorestaurantextendsLikeeat { Publicgotorestaurant (Eat Eat) {Super(EAT); } Public voidOnWay () {System.out.println ("On the way to the hotel."); } @Override Public voideat () {Super. Eat (); OnWay (); } } Public classInrestaurantextendsLikeeat { Publicinrestaurant (Eat Eat) {Super(EAT); } Public voidSelectfoot () {System.out.println ("The hotel is perfect."); } @Override Public voideat () {Super. Eat (); Selectfoot (); }} Public classEatfootextendsLikeeat { Publiceatfoot (Eat Eat) {Super(EAT); } Public voideating () {System.out.println ("Enjoy the food"); } @Override Public voideat () {Super. Eat (); Eating (); }} Public classEndeatextendsLikeeat { Publicendeat (Eat Eat) {Super(EAT); } Public voidaftereat () {System.out.println ("===== Food ends ====="); } @Override Public voideat () {Super. Eat (); Aftereat (); }}
Create a test class to test the adornment effect
Public classEattest { Public Static voidMain (string[] args) {Eat person=NewPerson (); Likeeat likeeat=NewEndeat (NewEatfoot (NewInrestaurant (NewGotorestaurant (NewFindinmap (person))))) ; Likeeat.eat (); }}
Operation Result:
Summarize:
A,likeeat abstract class, holds the Eat interface, the method is all delegated to the interface call, the purpose is to give the interface implementation class that is the subclass to call.
B,likeeat abstract class subclass (specific decorator), there is a construction method called super (Eat), this sentence reflects the abstract class relies on the implementation of the subclass is abstract dependent on the principle of implementation. Because the structure inside the parameters are eat interface, as long as the implementation of the Eat class can be passed in, that is to show
Likeeat likeeat = new Endeat (
New Eatfoot (
New Inrestaurant (
New Gotorestaurant (
New Findinmap (person)))))
The look of this structure. So when calling Likeeat.eat () , and because of each specific decorator class, first call super.eat (); method, and the Super has been passed by the construction and pointed to a specific decorator class (this can be replaced according to the order), then called the Decoration class method, and then call their own decorative method, that is, a decorative, chain-like behavior of the filter.
C, the specific decoration, you can define the initial state or the initial self-decoration, the following decorative behavior on the basis of a step-by-step embellishment, decoration.
D, the decorator mode design principles are:
Open to the extension, the change is closed, this sentence is reflected in if I want to extend the behavior of the decorator class, without modifying the adorner abstract class, simply inherit the adorner abstract class, the implementation of additional decoration or call behavior can be decorated by packaging.
Decorator mode for Java design patterns