In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the Java decorator model". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the Java decorator pattern.
I. the definition and characteristics of decoration mode
In the software development process, you sometimes want to use some existing components. These components may only perform some core functions. However, without changing its structure, its function can be expanded dynamically. All of this can be done in decorator mode.
Just like when we cook, we need to use a series of abstract components such as seasoning, dish, knife, fire and so on to finally complete a dish.
Definition of Decoration pattern:
Refers to the pattern that dynamically adds some responsibilities (that is, additional functions) to the object without changing the existing object structure. it belongs to the object structure pattern. In terms of added functionality, the decoration pattern is more flexible than generating subclasses.
Features:
The decorator is a powerful supplement to inheritance and is more flexible than inheritance. It dynamically extends the function of an object, namely plug and play, without changing the original object.
Different effects can be achieved by using non-decorative classes and the arrangement and combination of these decorative classes.
The decorator mode fully complies with the principle of opening and closing
Shortcoming
The decorator pattern adds many subclasses, and overuse will increase the complexity of the program.
Second, the structure of the decoration pattern
The structure of the decoration pattern generally consists of the following roles
1. Abstract component (Component) role: defines an abstract interface to standardize objects that are ready to receive additional responsibilities.
two。 ConcreteComponent roles: implement abstract artifacts and add responsibilities to them by decorating roles.
3. Abstract decoration (Decorator) role: inherits abstract components and contains instances of concrete components, which can extend the functions of concrete components through their subclasses.
4. ConcreteDecorator roles: implement methods related to abstract decoration and add additional responsibilities to concrete component objects.
Illustration
Third, coffee order case demonstration
There is a demand, ordering a cup of coffee requires coffee, materials and so on. This case is very suitable for decoration mode, such as dressing, ordering food, buying steamed buns, and so on. How can we design it into a decoration mode?
This structure is a class diagram of a decorative pattern that I have designed, which is automatically generated by idea. The Drink here is the abstract build role we mentioned above, the decorator is Decorator, he is an abstract decoration, and his subclass below is the concrete decorator. Then we provide an intermediate build in the middle of the concrete build, which provides some commonalities of coffee, which can be put here and inherited directly when used. The following are the corresponding concrete components, the specific role of the decorated, the decorator and the decorated inherit from the component abstract components. What we need to decorate is that we order a cup of coffee, wrap it with decoration, and wrap it layer by layer. The example is as follows:
For example, I would like to order a latte with sugar and milk
Code example:
Component Abstract component roles:
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1.0 * @ packageName com.decoratorPattern.starBucks * @ className Drink * @ date 2021-12-28 10:28 * @ Description Beverage component Class Abstract component * / public abstract class Drink {private String description; private float price = 0.0f; public String getDescription () {return description;} public void setDescription (String description) {this.description = description } public float getPrice () {return price;} public void setPrice (float price) {this.price = price;} / * * @ Date 10:30 on 2021-12-28 * @ Param * @ Return float * @ MetodName cost * @ Author wang * @ Description calculate the cost, order price * / public abstract float cost ();}
Decorator class:
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1. 0 * @ packageName com.decoratorPattern.starBucks * @ className Decorator * @ date 2021-12-28 10:40 * @ Description decorator defines class, ingredient * / public class Decorator extends Drink {private Drink drink / * @ param drink * @ Date on 2021-12-28 10:42 * @ Param * @ Return null * @ Author wang * @ Description introduce a decorated person to be decorated by the decorator * / public Decorator (Drink drink) {this.drink = drink } / * * @ Date 10:43 on 2021-12-28 * @ Param * @ Return float * @ Author wang * @ Description the price of the decorator plus the price of the decorated * / @ Override public float cost () {return super.getPrice () + drink.cost () } / * @ Date 10:44 on 2021-12-28 * @ Param * @ Return String * @ MetodName getDescription * @ Author wang * @ Description output order information, including decorator, decorator's price And the information of the decorated person * / @ Override public String getDescription () {return drink.getDescription () + "\ n added material:" + super.getDescription () + "\ t material Price:" + super.getPrice () }}
Decorator class:
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1. 0 * @ packageName com.decoratorPattern.starBucks * @ className Decorator * @ date 2021-12-28 10:40 * @ Description decorator defines class, ingredient * / public class Decorator extends Drink {private Drink drink / * @ param drink * @ Date on 2021-12-28 10:42 * @ Param * @ Return null * @ Author wang * @ Description introduce a decorated person to be decorated by the decorator * / public Decorator (Drink drink) {this.drink = drink } / * * @ Date 10:43 on 2021-12-28 * @ Param * @ Return float * @ Author wang * @ Description the price of the decorator plus the price of the decorated * / @ Override public float cost () {return super.getPrice () + drink.cost () } / * @ Date 10:44 on 2021-12-28 * @ Param * @ Return String * @ MetodName getDescription * @ Author wang * @ Description output order information, including decorator, decorator's price And the information of the decorated person * / @ Override public String getDescription () {return drink.getDescription () + "\ n added material:" + super.getDescription () + "\ t material Price:" + super.getPrice () }}
Specific component class: latte
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1.0 * @ packageName com.decoratorPattern.starBucks * @ className latte * @ date 10:32 on 2021-12-28 * @ Description latte, decorated by * / public class Latte extends Coffee {public Latte () {setDescription ("latte"); setPrice (15.0f);}}
Specific component class: mocha
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1.0 * @ packageName com.decoratorPattern.starBucks * @ className Mocha * @ date 10:36 on 2021-12-28 * @ Description mocha, decorated by * / public class Mocha extends Coffee {public Mocha () {setDescription ("mocha"); setPrice (12.2f);}}
Others are the same as above, but show more
Specific decoration category: milk
Package com.decoratorPattern.starBucks / * @ author wang * @ version 1.0 * @ packageName com.decoratorPattern.starBucks * @ className Milk * @ date 2021-12-28 10:47 * @ Description Milk condiment Specific decorator * / public class Milk extends Decorator {/ * * @ param drink * @ Date 10:42 on 2021-12-28 * @ Param * @ Return null * @ MetodName Decorator * @ Author wang * @ Description introduce a decorated person, and the decorator will decorate * / public Milk (Drink drink) {super (drink) SetDescription ("milk"); setPrice (1.0f);}}
Specific decoration: sugar
Package com.decoratorPattern.starBucks / * @ author wang * @ version 1.0 * @ packageName com.decoratorPattern.starBucks * @ className sugar * @ date 10:50 on 2021-12-28 * @ Description Sugar, decorator * / public class Sugar extends Decorator {/ * * @ param drink * @ Date 2021-12-28 10:42 * @ Param * @ MetodName Decorator * @ Author wang * @ Description introduce a decorated person Decorated by the decorator * / public Sugar (Drink drink) {super (drink) SetDescription ("sugar"); setPrice (0.5f);}}
Order test code:
Package com.decoratorPattern.starBucks; / * * @ author wang * @ version 1. 0 * @ packageName com.decoratorPattern.starBucks * @ className OrderTest * @ date 2021-12-28 10:51 * @ Description Front desk order Class * / public class OrderTest {public static void main (String [] args) {/ / order a latte System.out.println with sugar and milk ("+ nothing +"); Drink latte = new Latte () System.out.println ("current Total Price:" + latte.cost ()); System.out.println ("coffee:" + latte.getDescription ()); / / sweetened System.out.println ("+ added Sugar +"); latte = new Sugar (latte); System.out.println ("current Total Price:" + latte.cost ()); System.out.println ("coffee:" + latte.getDescription ()) System.out.println ("+ after adding milk +"); latte = new Milk (latte); System.out.println ("current Total Price:" + latte.cost ()); System.out.println ("coffee:" + latte.getDescription ()) Coffee: latte * + with sugar + * current total price: coffee: latte * added material: sugar material price: 0.5 * + milk + * current total price: 16.5 * coffee: latte * material price: sugar material price: 0. 5 * added material: milk material price: 1. 0 * * Process finished with exit code 0 * /
To sum up, if we need a new type of coffee or a new sauce, we just need to add a new class to inherit the coffee or decorator class.
At this point, I believe you have a deeper understanding of "how to understand the Java decorator model". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.