In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "JAVA decorator pattern application example analysis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "JAVA decorator pattern application example analysis" it!
What is the decorator mode?
Definition of Decorator pattern: a pattern that dynamically adds some responsibilities (that is, additional functions) to the object without changing the existing object structure, which belongs to the object structure pattern.
Advantages
1. The decorator is a powerful supplement to inheritance, which is more flexible than inheritance. It dynamically extends the function of an object, namely plug and play, without changing the original object.
2. Different effects can be achieved by using non-decorative classes and the arrangement and combination of these decorative classes.
3. The decorator mode fully obeys the principle of opening and closing.
Shortcoming
The decorator pattern adds many subclasses, and overuse will increase the complexity of the program.
Knowledge point
In general, the ability to extend a class is implemented by inheritance. But inheritance has static characteristics, high coupling, and with the increase of extended functions, the subclasses will be very inflated. If you use a composite relationship to create a wrapper object (that is, a decoration object) to wrap the real object and provide it with additional functionality while keeping the class structure of the real object unchanged, this is the goal of the decorator pattern. The following is to analyze its basic structure and implementation method.
Decorator pattern implementation
Case study: the Black Tiger caught the Seven and roasted the rainbow cat.
Abstract component (Component) roles: seven
Concrete component (ConcreteComponent) role: rainbow cat
Abstract Decoration (Decorator) characters: feeding
Specific decoration (ConcreteDecorator) role: add salt, add cumin.
The Seven
Seven interface, declaring an abstract method of barbecue
Public interface Qi {void show ();} rainbow cat
The rainbow cat class implements the seven-man interface and realizes the method of roasting the rainbow cat.
Public class Hong implements Qi {@ Override public void show () {System.out.println ("barbecue a rainbow cat");}} add ingredients
This is an abstract decorator that implements the seven-man interface, declares a seven-man attribute, and invokes the seven-man barbecue method.
Public class JiaLiao implements Qi {private Qi qi; JiaLiao () {} JiaLiao (Qi component) {this.qi = component;} @ Override public void show () {qi.show ();}} add salt
Inherited the abstract class, and rewrote the barbecue method, adding a salt
Public class Yan extends JiaLiao {private Qi qi; Yan () {} Yan (Qi qi) {super (qi);} @ Override public void show () {super.show (); add ();} public void add () {System.out.println ("salt");}} cumin
Inherited the abstract class and rewrote the barbecue method, adding an cumin
Public class ZiRan extends JiaLiao {private Qi qi; ZiRan () {} ZiRan (Qi qi) {super (qi);} @ Override public void show () {super.show (); add ();} public void add () {System.out.println ("Ghazi");}} Test
New a rainbow cat, roast it.
Add some salt
Add some salt and cumin
Public class Demo {public static void main (String [] args) {Qi qi = new Hong (); qi.show (); System.out.println (); Qi qi1 = new Yan (qi); qi1.show (); System.out.println (); Qi qi2 = new ZiRan (qi1); qi2.show ();}}
At this point, I believe that everyone on the "JAVA decorator pattern application example analysis" have a deeper understanding, might as well to the actual operation of it! 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.