Java modifier mode and java decoration Mode
Head First design pattern LearningDecoration Mode
- Dynamically attaches the responsibility to the object. To expand the function, the modifier provides an alternative solution that is more flexible than inheritance.
Class Diagram
Participants
1. Component (base class of the decorated object)
Define an object interface to dynamically add responsibilities to these objects.
2. ConcreteComponent (specific object to be decorated)
Define an object and add some responsibilities to this object.
3. Decorator (modifier abstract class)
Maintain a reference pointing to the Component instance and define an interface consistent with the Component interface.
4. ConcreteDecorator (Specific modifier)
The specific decoration object adds specific responsibilities to the specific decoration object held internally.
Involved roles
- Abstract component: defines an abstract interface to standardize the classes for preparing additional functions.
- Specific components: classes to be added with functions to implement the abstract component role Interface
- Abstract modifier: holds a reference to a specific component role and defines an interface consistent with the abstract component role.
- Specific Decoration: implements the abstract modifier role and adds additional functions to specific components.
Example:
I often go to a restaurant to eat beef noodles. You can choose to add eggs, ham, Bean Skin and other side dishes. This can be done in the modifier mode.
Component (base class of the decorated object)
package com.Observer.model;public abstract class food {String size;String description;public String getSize() {return size;}public void setSize(String size) {this.size = size;}public String getDescription() {return description;}public abstract double cost();}
ConcreteComponent (specific object to be decorated)
Package com. observer. model; public class BeefNoodles extends food {public BeefNoodles () {description = "beef noodle bowl" ;}@ Overridepublic double cost () {if (super. getSize (). equals ("bowl") {super. description = "beef noodle bowl"; return 12.0;} super. description = "beef noodle bowl"; return 10.0 ;}}
Package com. Observer. model; public class Casserole extends food {public Casserole () {description = "Casserole instant noodle" ;}@ Overridepublic double cost () {return 12.0 ;}}
Decorator (modifier abstract class)
package com.Observer.model;public abstract class Condiment extends food {public abstract String getDescription();}
ConcreteDecorator)
Package com. observer. model; public class Ham extends Condiment {food; public Ham (food) {this. food = food ;}@ Overridepublic String getDescription () {return food. getDescription () + ", ham" ;}@ Overridepublic double cost () {return 2.0 + food. cost ();}}
Package com. observer. model; public class Egg extends Condiment {food; public Egg (food) {this. food = food ;}@ Overridepublic String getDescription () {return food. getDescription () + ", egg" ;}@ Overridepublic double cost () {return 1.5 + food. cost ();}}
Package com. observer. model; public class Rosa extends Condiment {food; public Rosa (food) {this. food = food ;}@ Overridepublic String getDescription () {return food. getDescription () + ", ";}@ Overridepublic double cost () {return 1.0 + food. cost ();}}
Test
Package com. observer. model; public class Test {public static void main (String [] args) {food = new Casserole (); System. out. println (food. getDescription () + "price," + food. cost () + "RMB"); food food1 = new BeefNoodles (); food1.setSize ("bowl"); food1 = new Ham (food1); food1 = new Ham (food1 ); food1 = new Egg (food1); food1 = new Rosa (food1); System. out. println (food1.getDescription () + "price," + food1.cost () + "Yuan"); food food2 = new BeefNoodles (); food2.setSize ("small bowl "); food2 = new Ham (food2); food2 = new Ham (food2); food2 = new Egg (food2); food2 = new Rosa (food2); System. out. println (food2.getDescription () + "price," + food2.cost () + "Yuan ");}}
Result
Casserole instant noodles price, 12.0 yuan beef noodle bowl, ham, ham, eggs, Bean Skin price, 18.5 yuan beef noodle bowl, ham, ham, eggs, Bean Skin price, 16.5 yuan
Key points:
- Inheritance is one of the extensions, but it is not necessarily the best solution to achieve elastic design.
- In our design, behavior should be allowed to be extended without having to modify the existing code.
- Combinations and delegation can be used to dynamically add new behaviors during runtime.
- In addition to inheritance, the modifier mode also allows us to expand our behavior.
- The decorator Mode means a group of decorator classes that are used to wrap specific components.
- The modifier class reflects the component type to be decorated (in fact, they have the same type and are implemented through interfaces or inheritance ).
- You can add your own actions before/or after the actions of the decorator, or even replace the actions of the decorator for a specific purpose.
- You can package a widget with or without a number of decorators.
- The decorator is generally transparent to the customer, unless the customer program depends on the specific type of components.
Reference blog: https://www.cnblogs.com/chenxing818/p/4705919.html