Detailed introduction of decoration mode of Java design mode

1. Definition of decorator: also known as wrapper mode, decoration mode extends the function of objects in a transparent way to the client, which is an alternative to inheritance.

2. Decoration mode dynamically adds more responsibilities to an object in a transparent way to the client. In other words, the client does not know the difference between the object before and after decoration.

3. Decoration mode can expand the function of objects without creating more subclasses.

4. Difference between decoration mode and class inheritance:

1) Decoration pattern is a dynamic behavior that combines existing classes at will, and class inheritance is a static behavior. What kind of class is defined, the object of this class has what kind of function, which can not be changed dynamically.

2) Decoration mode extends the function of an object without increasing the number of classes. Class inheritance extension is the function of a class. In the inheritance relationship, if we want to add the function of an object, we can only add two methods to the subclass through the inheritance relationship.

3) Comparison of decoration and inheritance:

4) Decoration mode dynamically extends the function of an object without changing the original class file and using inheritance. It wraps the real object by creating a packaging object, that is, decoration.

5. Decoration mode delegates the call to the client to the decorated class. The key of decoration mode is that this extension is completely transparent.

6. Composition of decoration mode:

1) Abstract build role (component): give an abstract interface to standardize objects ready to accept additional responsibilities. It is equivalent to InputStream / OutputStream and reader / writer in I / O stream.

2) Concrete component: defines a class that will accept additional responsibilities. It is equivalent to fileoutputstream and FileInputStream in I / O.

3) Decorator: holds a reference to an abstract component role and defines an interface consistent with the abstract component. It is equivalent to fileroutputstream and filterinputstream in I / O.

4) Concrete decorator: responsible for "pasting" additional responsibilities to the construction object. It is equivalent to bufferedoutputstream and bufferedinputstream, dataoutputstream and datainputsrtream in the I / O stream.

7. Characteristics of decoration mode:

1) Decorative objects and real objects have the same interface, so that client objects can interact with decorative objects in the same way as real objects.

2) A decorated object contains a reference to a real object

3) The decorated object accepts all requests from the client and forwards them to the real object.

4) Decoration objects can add some additional functions before or after forwarding these requests. This ensures that additional functions can be added externally without modifying the given object structure at run time. In object-oriented programming, inheritance is usually used to extend the functions of a given class.

8. Case:

1) Abstract build interface:

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
分享
二维码
< <上一篇
下一篇>>