Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does decorator mode mean?

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 decorator mode". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the decorator mode means.

Decorator pattern refers to the pattern that dynamically adds some responsibilities (i.e. additional functions) to the object without changing the existing object structure. it belongs to the object structure pattern. The use of decoration mode to expand the function of objects is more flexible than inheritance; many different specific decoration classes can be designed to create a combination of different behaviors. But the decoration mode adds many subclasses, which can complicate the program if overused.

Pattern structure

The roles of the decorator mode are as follows:

Top-level interface

The decorated

Decorator abstract class

Decorator

Source code guide

The typical use of the decorator pattern is the io stream. As we mentioned earlier in the adapter pattern, the io stream uses the adapter pattern for converting byte streams to character streams; the decorator pattern is also a classic use in IO streams. Where it is used is to decorate the stream with a cache. Take the input stream as an example. BufferedInputStream is the decoration of FileInputStream. Let's take a look at this code:

String file = "out.txt"; InputStream ins = new FileInputStream (file); BufferedInputStream bufin= new BufferedInputStream (ins); int b; while ((b=bufin.read ())! =-1) {System.out.println (Integer.toHexString (b));}

BufferedInputStream source code:

Public class BufferedInputStream extends FilterInputStream {public BufferedInputStream (InputStream in) {this (in, DEFAULT_BUFFER_SIZE);} Public synchronized int read (byte b [], int off, int len) throws IOException {. }}

It inherits FilterInputStream, while FilterInputStream inherits InputStream,FileInputStream and InputStream. So the role relationship here is that InputStream is the top-level class (interface), FileInputStream is the decorated class, and BufferedInputStream is the decorative class. BufferedInputStream decorates the method that FileInputStream inherited from InputStream, here. The role FilterInputStream occupies is the decorator abstract class, but it is not an abstract class; this does not affect our understanding of its pattern.

At this point, I believe you have a deeper understanding of "what the decorator mode means". You might as well do it in practice. 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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report