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

How to implement the java decorator pattern

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to realize the java decorator pattern". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to implement the java decorator pattern.

Definition:

Dynamically extend the functionality of an object without changing the original class file and the inheritance used by the original class.

It is achieved by creating a wrapper object, that is, wrapping the real object with decoration.

Roles:

Abstract component role (Project): gives an interface to standardize objects that are ready to receive additional responsibilities.

Concrete component roles (Employe): define a class that will receive additional responsibilities.

Manager: holds an instance of a component object and defines an interface consistent with the abstract component interface.

Specific decoration roles (ManagerA, ManagerB): responsible for "affixing" additional responsibilities to component objects.

Example:

Public interface:

Public interface Person {void eat ();}

Object to be decorated:

Public class OldPerson implements Person {@ Override public void eat () {System.out.println ("eat");}}

Decoration object:

Public class NewPerson implements Person {private OldPerson p; NewPerson (OldPerson p) {this.p = p;} @ Override public void eat () {System.out.println ("making a fire"); System.out.println ("cooking"); p.eat (); System.out.println ("washing dishes");}}

Test:

Public class PersonDemo {public static void main (String [] args) {OldPerson old = new OldPerson (); / / old.eat (); NewPerson np = new NewPerson (old); np.eat ();}}

Through the example, we can see that the original OldPerson class is not changed, and its subclass is not defined, but the extension of Person is implemented, which is the role of the decorator pattern.

Advantages:

1. Using the decorator pattern is more flexible than using inheritance, because it chooses to extend the functionality of an object in a dynamic way, and you can choose different decorators at run time to achieve different behaviors.

2, through the use of different specific decoration classes and the arrangement and combination of these decoration classes, many combinations of different behaviors can be created. You can use multiple concrete decoration classes to decorate the same object to get more powerful objects.

3, the concrete component class and the concrete decoration class can be changed independently, and it can be low coupling. Users can add new specific component classes and specific decoration classes according to their needs, and then make a variety of combinations when using them, and the original code does not need to be changed, in line with the "opening and closing principle".

Disadvantages:

1, it will produce a lot of small objects, which increases the complexity of the system.

2. This feature, which is more flexible than inheritance, also means that decoration mode is more prone to errors and difficult to troubleshoot than inheritance. For objects decorated many times, finding errors during debugging may need to be checked step by step, which is more cumbersome.

The difference between the decorator and the adaptor model:

1, the adapter pattern is mainly used to be compatible with classes that do not work together, so that they can be compatible with the target interface, although it is possible to add new responsibilities as decorators, but this is not the goal.

The decorator model is mainly to add new responsibilities to the decorated.

2. The adapter pattern calls the original interface with the new interface, which is not visible or available to the new system.

The decorator mode uses the original interface intact, and the system also uses the original interface for the decorative objects.

3, the adapter knows the details of the adaptor (that is, that class or interface).

The decorator only knows what its interface is, and its specific type (whether it is a base class or other derived class) is known only at run time.

The difference between decorator and inheritance:

Inheritance:

Advantages: the code structure is clear and the implementation is simple

Disadvantages: for each class that needs to be enhanced, you have to create specific subclasses to help it enhance, which will lead to an overly large inheritance system.

Decorator:

Advantages: multiple classes that need to be enhanced can be enhanced internally by polymorphic technology

Cons: instances of classes that need to be enhanced need to be maintained internally through polymorphic techniques. This in turn makes the code a little more complicated.

Use the scene:

1, you need to extend the functionality of a class, or add additional responsibilities to a class.

2, you need to add functions to an object dynamically, which may be ambiguous or temporary, and can be dynamically revoked at any time.

3, it is necessary to add a very large number of functions generated by the arrangement and combination of some basic functions, so as to make the inheritance relationship unrealistic.

4. When the method of generating subclasses cannot be used for extension. In one case, there may be a large number of independent extensions, which will produce a large number of subclasses to support each combination, resulting in an explosive increase in the number of subclasses. Another situation may be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

At this point, I believe you have a deeper understanding of "how to achieve the java decorator pattern". 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