Detailed examples of design patterns (decoration design patterns) in Java

Detailed examples of design patterns (decoration design patterns) in Java

Application scenario:

Without modifying the original object class, it provides enhanced additional functions to one or more existing class objects

I think it can be understood literally, decorate, decorate the house. The house can be regarded as the original class. It means that you decorate an already built house according to your own ideas. Inheritance can also achieve such functions, but inheritance has its disadvantages. Inheritance is only a single inheritance. The decoration design pattern can take different functions of multiple different classes.

Specific steps:

◎ step 1: pass the class to be strengthened by constructing the parameter. (if you want to decorate your house, you must have a house first. This is easy to understand)

◎ step 2: write down the specific functions to be enhanced

Specific examples and codes

Write a mybufferedreader class to enable it to enhance character streams (such as FileReader, inputstreamreader, pipedreader, etc.):

(1) Provide a buffered myread() method to speed up the original read() method;

(2) Provides a myreadline() method that can read one line of characters at a time.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>