In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 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 "what is the decoration mode in Java". In the operation of actual cases, many people will encounter such a 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!
The role of decorator mode is to enhance the target method, provide new functions or additional functions, dynamically expand objects, is one of the alternative methods of inheritance relationship, in line with the opening and closing principle of Java development. The decorator model is very similar to the agent model, which can help you understand and learn better.
We need to enhance it by implementing the interface, so the goal must be abstracted as an interface.
Composition:
Abstract component (Component): a specification that defines decoration methods
ConcreteComponent: the concrete implementation of Component, that is, the specific object we want to decorate.
Decorator Decorator: holds an instance reference to a component (Component) object, which is responsible for decorating specific component objects and defining specifications.
Specific decoration (ConcreteDecorator): responsible for decorating component objects with additional functions.
Define an abstract interface
Public interface Train {void speed ();}
Define decorator
Public class Decorator implements Train {private Train train; public Decorator (Train train) {this.train = train;} @ Override public void speed () {train.speed ();}
Define specific decoration types
First kind
Public class HeXie extends Decorator {public HeXie (Train train) {/ / calls the parameter construction super (train) of the parent class;} public void speedUp () {System.out.println ("Speed up to 300km/h, change to Harmony High Speed Rail");} @ Override public void speed () {super.speed (); speedUp ();}}
The second kind
Public class FuXing extends Decorator {public FuXing (Train train) {/ / calls the parameter construction super (train) of the parent class;} public void speedUp () {System.out.println ("speed up to 350km/h, change to Fuxing high-speed rail");} @ Override public void speed () {super.speed (); speedUp ();}}
Define the decorated
Public class PuKuai implements Train {@ Override public void speed () {System.out.println ("ordinary train speed 100km/h");}}
test
Public class Demo {@ Test public void test1 () {PuKuai puKuai = new PuKuai (); Decorator decorator = new HeXie (puKuai); decorator.speed (); System.out.println ("- -"); Decorator decorator1 = new FuXing (new HeXie (puKuai)); decorator1.speed () }}
Result
Ordinary train speed 100km/h increases to 300km/h, becomes Harmony high-speed railway-- ordinary train speed 100km/h increases speed to 300km/h, Harmony high-speed train increases speed to 350km/h, and Fuxing high-speed railway Process finished with exit code 0
You can wrap an object with one or more ConcreteDecorator.
The decorator can add his own behavior before and / or after the behavior of the entrusted decorator in order to achieve a specific purpose. (similar to proxy mode)
The design principle of the decorator pattern is: open to extension and closed to modification. This sentence is reflected in that if I want to extend the behavior of the decorated class, I do not need to modify the decorator base class, but only need to inherit the decorator base class. implement some additional decoration or behavior to package the decorated person. So: extension is embodied in inheritance and modification in subclasses, rather than specific abstract classes, which fully embodies the principle of dependency inversion.
The target class can provide services to the outside world, while the target class of the agent model does not provide services, all of which are completed by the agent class.
Java's I _ big O API is implemented using Decorator.
Abstract component (InputStream): a superclass in the decorator pattern that has only one abstract method read (), which needs to be handled by subclasses.
Decorated (FileInputStream, ByteArrayInputStream, StringBufferInputStream): has a general basic method to implement read ()
Decorator component (FilterInputStream): defines the code of conduct of a specific decorator, which can be handled on a unified basis.
Specific decoration (BufferedInputStream, DataInputStream, Base64InputStream): specific decoration category, with convective read operation to complete the specific expansion ability.
This is the end of the content of "what is the Decoration pattern in Java". Thank you for your 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.