In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to realize the decorator pattern in the web design pattern". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to realize the decorator pattern in the web design pattern".
Schema definition
Decorator pattern belongs to structural design pattern. First of all, let's understand the definition of decorator pattern.
In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. The decorator pattern is often useful for adhering to the Single Responsibility Principle, as it allows functionality to be divided between classes with unique areas of concern.
In short, the decorator pattern is to dynamically extend additional functionality for an object, whether dynamic or static, without affecting the behavior of other similar objects, which makes the object more flexible at run time.
Decorators are usually implemented by combination or inheritance, which allows the client to decorate according to the requirements, so as to achieve the goal of increasing functions. To simplify understanding, the word decoration in the decorator pattern is actually adding additional functionality to the original object.
Pattern structure
Next let's take a look at the hierarchy and main roles of the decorator pattern.
Component abstract components, the most primitive, core objects, usually interfaces or abstract classes.
ConcreteComponent specific components, the implementation of Component, is also the object that needs to be decorated.
Decorator decorators, usually abstract implementations of abstract components, must have private variables pointing to Component.
ConcreteDecorator concrete decoration object, is the concrete implementation of Decorator, used to decorate the original, core methods enhanced classes.
Pattern realization
Now we use the example of drinking coffee to implement the decorator model, suppose we want to buy a cup of coffee, need to add sugar, milk, and nothing to add coffee and milk, the price of sugar is not the same, we have to calculate how much it will cost to make a cup of coffee with sugar and milk.
Here coffee is our specific component ConcerateComponent, milk and sugar is the specific decoration object ConcreteDecorator. For simplicity, let's first create a Component interface class, Drink, and define two methods to get the price and description:
Next, create a ConcreteComponent class Coffee to implement the Drink interface.
Now you need an abstract class DrinkDecorator to extend the implementation enhancements.
Then it is to implement the specific decoration objects Milk and Sugar.
Finally, the client implements the action of making coffee:
The following figure is a class diagram of an example:
As can be seen from the above example, by adding different materials, the price is different, and the description information is also different, and the order of adding can be changed dynamically, even without adding, making the object very flexible. This is the essence of the decorator pattern.
Mode usage scene
The decorator pattern is also widely used in JDK source code, as we can often see:
JDK's IO operation classes, such as a series of implementation classes of FilterInputStream under the java.io.InputStream architecture, such as BufferedInputStream,LineNumberInputStream,DataInpuStream.
Java.util.Collections#synchronizedXXX ()
The decorator pattern allows objects in the program to be enhanced and removed at run time, and extended in a more flexible combination.
The gain and loss of the pattern
Advantages:
Dynamically add functionality to a single object without affecting other objects.
Decoration class and decorated class (ConcreteComponent) are independent, uncoupled and easy to expand.
Instead of the functional implementation of inheritance, reduce the existence of inheritance classes.
Disadvantages:
Too many decoration layers can make decorated objects more complex and difficult to understand, such as when using the DataInputStream object of Java I _ DataInputStream O to read data to the Java primitive type value:
DataInputStream is = new DataInputStream (new BufferedInputStream (new FileInputStream (file)
If there are too many decoration classes in the program, it is slightly difficult to understand and use.
Thank you for your reading, the above is the content of "how to achieve the decorator pattern in the web design pattern". After the study of this article, I believe you have a deeper understanding of how to achieve the decorator pattern in the web design pattern. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.