In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the basic concept and implementation of decorator pattern in java". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Basic concept
The decorator pattern, as its name implies, plays the role of decoration, which is to add functionality to a class. If you add functionality through inheritance, if you add more functionality without modifying the code, the number of classes will explode and bring great trouble for management. The decorator mode solves this point better.
Introduction
The following is a common class diagram for the decorator pattern:
Component, usually an interface or abstract class, defines the simplest method, which is implemented by both the decorator class and the decorated class.
ConcreteComponent, the decorated class, implements Component.
Decorator, the decorator class, which dynamically adds additional methods to ConcreteComponent, implements the Component interface, and holds a member variable of Component in the object.
ConcreteDecoratorA,ConcreteDecoratorB, the concrete decoration class, the method in this class is the method that you want to add dynamically for ConcreteComponent.
Realize
Let's take the production of a piece of clothing as an example. It is a very simple process to produce a piece of clothing itself. After a piece of cloth is cut, it is OK to make the dress look like it, but such clothes cannot be sold because there is no sense of beauty. We need some decoration to make the clothes look good. But the times are changing, people's aesthetics are also changing, decoration is always changing, so we need to have a flexible mode to modify decoration.
Clothes.java
Public interface Clothes {public void makeClothes ();}
MakeClothes.java
Public class MakeClothes implements Clothes {@ Override public void makeClothes () {System.out.println ("make a dress");}}
Step 3 create the decorator.
OperationSubstract.java
Public class OperationSubstract implements Strategy {@ Override public int doOperation (int num1, int num2) {return num1-num2;}}
Needless to say, first to the initial finished product of clothes, is the kind that has no sense of beauty, then if you want to add decoration now, you can use a class to inherit MakeClothes, and then add the makeClothes () method inside, but if the decoration changes in a few days, then you have to change the code, and if you decorate too much, this class appears to be very complex and difficult to maintain, and this time the decorator mode will come to show its skill.
Decorator.java
Public class Decorator implements Clothes {private Clothes clothes; public Decorator (Clothes _ clothes) {this.clothes = _ clothes;} @ Override public void makeClothes () {clothes.makeClothes ();}}
This is a decorator that has a constructor, an argument to a clothing class, and it overrides the makeClothes () method so that its subclass can modify it. Here are two subcategories, embroidering and hollowing out the clothes.
Embroidery.java
Public class Embroidery extends Decorator {public Embroidery (Clothes _ clothes) {super (_ clothes);} public void embroidery () {System.out.println ("embroidery");} @ Override public void makeClothes () {super.makeClothes (); this.embroidery ();}}
Hollow.java
Public class Hollow extends Decorator {public Hollow (Clothes _ clothes) {super (_ clothes);} public void hollow () {System.out.println ("key location hollowing out");} @ Override public void makeClothes () {super.makeClothes (); this.hollow ();}}
The constructors of these two subclasses are introduced into a clothing model, and the two subclasses have their own methods-embroidery and hollowed-out, but they both rewrite the makeClothes () method, adding embroidery and hollowing-out operations in the process of making clothes, so that we only need to add, delete and modify the subclasses of these decorators, we can complete a variety of different decorations, concise and clear at a glance. Here's a test:
DecoratorDemo.java
Public class DecoratorDemo {public static void main (String [] args) {Clothes clothes = new MakeClothes (); clothes = new Embroidery (clothes); clothes = new Hollow (clothes); clothes.makeClothes (); System.out.println ("clothes are ready");}}
Execute the program and output the result:
Make a dress for embroidery key position hollowed-out clothes finished "the basic concept and implementation of the decorator pattern in java" is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.