In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what is the decorator mode of 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!
I. the basic introduction of the decorator model 1.1 intention
Dynamically add additional responsibilities to an object.
1.2 structure
The basic structure of the decorator pattern is as follows:
Component
Define an object interface to which you can dynamically add responsibilities.
ConcreteComponent
Define an object to which you can add some responsibilities.
Decorator
Decorate the role, hold an instance of the Component object, and define an interface consistent with the Componnet interface.
ConcreteDecorator
Specific decoration role, responsible for adding responsibilities to the component.
Second, the decorator pattern example
Sun WuKong in Journey to the West, with 72 changes, left a deep impression on us, and each of his changes brought him a new ability. When he becomes a fish, he can swim in the water; when he becomes a sparrow, he can fly in the sky. Let's use the example of Qi Tian Sheng to complete an example of the decorator pattern:
TheGreatestSage (Component)
Package com.wangmengjun.tutorial.designpattern.decorator
Public interface TheGreatestSage {
Void move ();}
Change (Decorator)
Package com.wangmengjun.tutorial.designpattern.decorator
Public class Change implements TheGreatestSage {
Private TheGreatestSage sage; public Change (TheGreatestSage sage) {this.sage = sage;}
Public void move () {sage.move ();}
}
Monkey (ConcreteComponent)
Package com.wangmengjun.tutorial.designpattern.decorator
Public class Monkey implements TheGreatestSage {
Public void move () {System.out.println ("Monkey move~~");}
}
Bird (ConcreteDecorator1)
Package com.wangmengjun.tutorial.designpattern.decorator
Public class Bird extends Change {
Public Bird (TheGreatestSage sage) {super (sage);}
@ Override public void move () {System.out.println ("Bird move~~");}}
Fish (ConcreteDecorator2)
Package com.wangmengjun.tutorial.designpattern.decorator
Public class Fish extends Change {
Public Fish (TheGreatestSage sage) {super (sage);}
@ Override public void move () {System.out.println ("Fish move~~");}}
Object wrapper example: package com.wangmengjun.tutorial.designpattern.decorator
Public class Client {public static void main (String [] args) {TheGreatestSage sage = new Monkey (); / * * single layer package * / TheGreatestSage bird = new Bird (sage); / / bird.move (); TheGreatestSage fish = new Fish (bird); / / fish.move (); / * * Multi-layer package * / TheGreatestSage fish3 = new Fish (new Bird (sage)) Fish3.move ();}
}
Is this approach deja vu?
Recall whether the same is true of how io streams are created in JDK. The simple structure of an InpuStream is as follows:
New BufferedReader (new FileReader ("F:\\ test.txt"))
III. Summary
3.1 applicable scenarios
Decoration mode can be used in the following situations:
You need to extend the functionality of a class, or add additional responsibilities to a class.
You need to add functions to an object dynamically, and these functions can be undone dynamically.
It is necessary to add a very large number of functions generated by the permutation and combination of some basic functions, so as to make inheritance relationships unrealistic.
3.2 advantages and disadvantages of vs
Advantages
The purpose of decoration pattern and inheritance relationship is to extend the function of objects, but decoration mode can provide more flexibility than inheritance. Decoration mode allows the system to dynamically decide to "paste" a needed "decoration" or remove an unwanted "decoration". The inheritance relationship is different, the inheritance relationship is static, it is determined before the system runs, and can not be changed at run time.
The decorator pattern does not attempt to support all predictable features in a complex customizable class. Instead, you can define a simple class and use the Decorator class to gradually add functionality to it. Complex functions can be combined from a simple component. You can create combinations of many different behaviors.
Shortcoming
Using decoration mode results in more objects than inheritance relationships, which look similar. More objects can make error checking difficult, especially since they all look alike.
This is the end of the content of "what is the decorator mode of java". 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.