Design Pattern-modifier pattern (JAVA) 2. Design Pattern-java
The last time I briefly talked about what is the decorator mode and some features of the decorator mode, this time I will talk about the application of the decorator mode.
I,Main involved classes or interfaces
1. component: the class inherited by the modifier and the modifier. It defines the methods to be implemented by the modifier and the modifier. It can be used independently or wrapped up by the modifier.
2. concreteComponent: allows the modifier to add a function to the object, that is, the object to be modified or the object to be dynamically added with a new line.
3. Decorator: it is a class with specific decorative functions. It is used to modify the Decorator. It can be an abstract class or an interface. It is an interface jointly implemented by all decorators.
Ii. Legends of the decorator Mode
If I have limited drawing capabilities, I will not plot it myself. This is from scratch,
On the way, component provides interfaces. In fact, the abstract class can also be used. In addition, a modifier can be decorated by multiple decorators, therefore, you can add a ConcreteDecorator next to the bottom graph.
Decorator1 to modify ConcreteDecorator. Of course, Decorator1 also inherits component.
Iii. instance applications
The weather is good today. We need a component to prepare an ice cream. The Code is as follows:
/*** Component --- the class that the decorator and the decorator must inherit **/public abstract class IceCream {public abstract void makeIceCream ();}
The Code is as follows:
Public class MakeIceCream extends IceCream {@ Override public void makeIceCream () {System. out. println ("make an ice cream ");}}
Decorator:
public abstract class DecoratIceCream extends IceCream{ @Override public abstract void makeIceCream(); }
Then there is ConcreteDecorator. Of course, ConcreteDecorator must inherit the common Decorator. Below are two ConcreteDecorator:
Public class FruitIceCream extends DecoratIceCream {IceCream iceCream; public FruitIceCream (IceCream iceCream) {this. iceCream = iceCream;} @ Override public void makeIceCream () {this. iceCream. makeIceCream (); System. out. println ("added fruit ");}}
Public class ChocolateIceCream extends DecoratIceCream {IceCream iceCream; public ChocolateIceCream (IceCream iceCream IceCream) {this. iceCream = iceCream;} @ Override public void makeIceCream () {this. iceCream. makeIceCream (); System. out. println ("Chocolate added ");}}
Next we can test:
Public static void main (String [] args) {System. out. println ("test the decorator mode ..... ");/*** Test the decorator -- you can use */IceCream ic = new MakeIceCream (); ic. makeIceCream (); System. out. println ("");/*** add only one modifier */DecoratIceCream dic = new FruitIceCream (ic); System. out. println ("Start with testing a single decorator... "); Dic. makeIceCream (); System. out. println (" test the end of a single modifier... "); System. out. println ("");/*** test adding multiple decorators */DecoratIceCream dic1 = new ChocolateIceCream (new FruitIceCream (ic); System. out. println ("test the start of multiple decorators... "); Dic1.makeIceCream (); System. out. println (" test multiple decorators... "); System. out. println ("");}
The running result is as follows:
We can see through the results that the independent component is also easy to use, and the modifier wrapped with component is also easy to use, and there can be more than one modifier.
Iv. Simplified Mode
1. If there is only one ConcreteDecorator, for example, this factory only produces fruit ice cream. How can this problem be solved?
In this case, ConcreteDecorator and Decorator can be merged. The merged model diagram is as follows (the source of the image is understandable ):
Below are the test classes:
public interface Component { public void makeIceCream();}
Public class ConcreteComponent implements Component {public void makeIceCream () {System. out. println ("make an ice cream ");}}
The modifier class does not change much. The code below shows that the modifier class does not change much:
Public class Decorator implements Component {Component component; public Decorator (Component component) {this. component = component;} public void makeIceCream () {component. makeIceCream (); System. out. println ("added cream ");}}
It was originally intended to produce fruit ice cream, but it was accidentally made into cream. You will eat it. Because only one type is produced, the Decorator does not need to inherit from other classes and can be directly integrated into one. The following test code:
Public class Main {public static void main (String [] args) {/*** test Component */Component c = new ConcreteComponent (); c. makeIceCream (); System. out. println ("");/*** after merging ConcreteDecorator and Decorator */Decorator d = new Decorator (c); d. makeIceCream (); System. out. println ("");}}
Running result:
2. Another simplified mode is that there is only one Concrete Component class without an abstract Component interface. In this case, the Decorator can inherit the Concrete Component. The model is as follows:
This is actually quite understandable. The two types mentioned above can produce a variety of products, such as producing ice cream and also producing ice cream, but this time I will directly produce soda, so no
After the abstract Component interface is used, I will not write the example, but it is actually very simple.
5. decorator Mode
1. You need to extend the functions of a class, or assign additional responsibilities to the class.
2. When adding a function to a class, it can be dynamic, rather than static. When the additional function is canceled, it is also dynamic.
3. add multiple functions to a class. These functions are attached in the form of arrangement and combination. A large number of subclasses are generated when an inherited method is used for appending, or the functions after the arrangement and combination cannot be implemented through inheritance;
Vi. Advantages and Disadvantages
1. The extended object functions are more flexible than inheritance, but the complexity is also increasing as the flexibility increases;
2. By using different specific decorative classes and the arrangement and combination of these decorative classes, designers can create a combination of many different behaviors. However, the decorator may lead to many small classes in the design. Excessive use may complicate the program.
3. The decoration mode is designed for the Component type. However, if you want to program specific components, you should rethink your application architecture and whether the decorator is appropriate. Of course, you can also change the Component Interface, add new public behavior, and implement the "Translucent" modifier mode.
VII. Design Principles
1. Multi-Purpose Combination, less inheritance.The behavior of inheriting child classes is determined statically at compilation, and all child classes will inherit the same behavior. However, if you can use a combination to expand the behavior of an object, you can dynamically scale it at runtime.
2. classes should be designed to be open to extensions and closed to modifications.This is the case for the time being. If you have any new problems in the application, continue writing.