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 mainly explains "what is the meaning of Java decorator mode". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the meaning of Java decorator mode".
I. definition
The design idea of the decorative pattern is mainly to dynamically extend the function of the object in a way transparent to the client, which is an alternative to the inheritance relationship (inheritance produces a large number of subclasses, and the code is redundant). The decoration pattern can extend the functionality of the object without creating more subclasses. The decorating mode delegates calls from the client to the decorated class. The key to the decoration pattern is that this extension is completely transparent (the transparency of the decoration pattern requires that client programs should not declare objects as concrete component types or concrete decoration types, but all as abstract component types). The application of decoration pattern is most significant in the I / O stream of java.
II. Role
Abstract component role (Component): usually an abstract class or an interface that defines a series of methods that can be implemented by subclasses or by themselves. You don't usually use this class directly, but implement specific functions by inheriting it or implementing the interface. (for example, for animals, there is an abstract way to output all the functions, the basic functions include: breathing, foraging, sleeping, etc.)
Concrete component role (Concrete Component): is a subclass of Component and implements the corresponding method, which is the decorated class. Specific construction characters can build many, such as dogs, cats, birds and so on, if it is a dog, we can decorate some roaring functions, meat-eating functions; birds can decorate some flying functions, with wings, and so on. Of course, these need to be specifically defined in the specific decorative roles)
Decorative role (Decorator): a subclass of Component, it is an abstract class (which can also be an interface) implemented by a concrete decorative role, and holds an object reference of type Component. Its main function is to delegate client calls to the decorated class.
Specific decorative role (Concrete Decorator): it is a specific decorative class, a subclass of Decorator and, of course, a subclass of Component. It is mainly to define specific decoration functions, such as the above, for the specific construction role of birds, in addition to the basic functions of abstract component roles, it also has some flight functions, with wing functions. Then we can define these two functions as a specific decorative role 1, for the specific component role of the dog, we can define the two functions of roaring and eating meat as a specific decorative role 2, so that if we define a specific component role such as a wolf, we can directly decorate with the specific decorative role 2.
III. Example
Write a code demonstration with the animal example mentioned above.
Abstract component roles (corresponding to animals)
Public interface Component {void function ();}
Specific component roles (corresponding to dogs)
Public class ConcreteComponent implements Component {@ Override public void function () {System.out.println ("basic function: breathing + foraging + sleeping");}}
Decorative role
Public class Decorator implements Component {private Component component; / / holds an object reference of type Component public Decorator (Component component) {this.component = component;} @ Override public void function () {component.function (); / / calls from the client delegate to a specific subclass}}
Specific decorative roles (corresponding to the two functions of roaring and eating meat)
Public class ConcreteDecorator extends Decorator {public ConcreteDecorator (Component component) {super (component);} @ Override public void function () {super.function (); System.out.println ("additional features:"); this.eat (); this.bellow () } private void eat () {System.out.println ("eat meat");} private void bellow () {System.out.println ("roar");}}
Client test:
Public class ClientTest {public static void main (String [] args) {Component component = new ConcreteComponent (); System.out.println ("- before decoration: -"); component.function (); Component newComponent = new ConcreteDecorator (component) System.out.println ("- after decoration: -"); newComponent.function ();}} output:-before decoration:-basic functions: breathing + foraging + sleeping-after decoration:-basic functions: breathing + foraging + sleeping additional functions: eating meat + roaring IV, application
The decorator pattern is mainly used in Java's Icano flow. If the reader is confused about the Ithumb O flow system, you might as well use the decorator pattern to sort out the train of thought. OutputStream and InputStream correspond to abstract component roles (Component), FileInputStream and FileOutputStream correspond to concrete component roles (ConcreteComponent), FilterOutputStream and FilterInputStream correspond to decorative roles (Decorator), and BufferedOutputStream,DataOutputStream and so on correspond to specific decorative roles.
Thank you for your reading, the above is the content of "what is the meaning of Java decorator mode". After the study of this article, I believe you have a deeper understanding of what Java decorator mode means, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.