In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you the example analysis of Java decorator pattern, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Define
Decorator mode: on the basis of not changing the original object, the function is dynamically attached to the object, which provides a more flexible alternative to inheritance and embodies the principle of opening and closing.
Case requirement
A man went to the coffee shop and ordered a cappuccino with hot milk.
Scheme
Define the coffee base class
Public abstract class Coffee {private String desc; private float price; public abstract float cost (); public String getDesc () {return desc;} public void setDesc (String desc) {this.desc = desc;} public float getPrice () {return price;} public void setPrice (float price) {this.price = price;}}
Define cappuccino coffee class and inherit coffee base class
Public class Cappuccino extends Coffee {public Cappuccino () {setDesc ("ordered a cappuccino"); setPrice (100);} @ Override public float cost () {System.out.println ("current price:" super.getPrice ()); return super.getPrice ();}} "
Define the decorator class
Public class Decorator extends Coffee {private Coffee coffee; public Decorator (Coffee coffee) {this.coffee = coffee;} @ Override public float cost () {return super.getPrice () + this.coffee.cost ();} @ Override public String getDesc () {return super.getDesc () + coffee.cost ();}
Define hot milk
Public class HotMilk extends Decorator {public HotMilk (Coffee coffee) {super (coffee); setPrice; setDesc ("ordered a glass of hot milk");}}
Define test classes
Public class Test {public static void main (String [] args) {/ / ordered a cappuccino with hot milk Coffee coffee = new Cappuccino (); System.out.println (coffee.getDesc ()); / / System.out.println (coffee.getPrice ()); System.out.println (coffee.cost ()); HotMilk hotMilk = new HotMilk (coffee) System.out.println (hotMilk.getDesc ()); / / System.out.println (hotMilk.getPrice ()); System.out.println (hotMilk.cost ());}}
View test results
Analysis.
The purpose of the decorator pattern is also to expand the function of the object, and it is an alternative mode of inheritance, which can dynamically expand the function of an implementation class. The decoration class and the decorated class can be independent of each other, not coupled, flexible and convenient.
Working with scen
You need to add responsibilities to an existing class, but there are times when you can't extend it by generating subclasses
When you need to combine an existing set of basic functions, which will produce a lot of functions.
When the functional requirements of the object can be dynamically added, or dynamically revoked
Knowledge point supplement
The definition and characteristics of Decoration pattern
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.
The structure of the decorative 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.
The above is all the content of the article "sample Analysis of Java decorator pattern". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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: 239
*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.