Design Mode-Decorator)

Source: Internet
Author: User

Design Mode-Decorator)
Decorator ):By dynamically adding some additional functions to an object, the decoration mode is more flexible than the subclass generation.
Instance: ConcreteComponent: allows the Decorator object to add functions for itself. Sometimes the derived class of ConcreteComponent provides core functions. In this case, ConcreteComponent is used to replace the Component function, and the modifier is a subclass inherited from ConcreteComponent.
Component: defines the methods to be implemented by the ConcreteComponent and Decorator classes. Simply put, if a class inherits from this class, it can be decorated or decorated. Decorator: class with specific decorative functions, used to decorate the ConcreteComponent class.

public abstract class Component {public abstract void Operation();}

 

public abstract class Component {public abstract void Operation();}
Public class ConcreteComponent extends Component {@ Overridepublic void Operation () {System. out. println (operations on specific objects !);}}
public abstract class Decorator extends Component {protected Component component;public void setComponent(Component component){this.component = component;}@Overridepublic void Operation() {if (component!=null) {component.Operation();}}}
Public class ConcreteDecoratorA extends Decorator {private String addedstate; @ Overridepublic void Operation () {super. Operation (); addedstate = New State !; System. out. println (Operation of decoration object !);}}
Public class ConcreteDecoratorB extends Decorator {@ Overridepublic void Operation () {super. Operation (); addedBehavior (); System. out. println (Operation of decoration object B !);} Private void addedBehavior () {// different from others }}
Public class Client {public static void main (String [] args) {ConcreteComponent c = new ConcreteComponent (); ConcreteDecoratorA ca = new ConcreteDecoratorA (); ConcreteDecoratorB cb = new release (); // ca packs c cb decoration ca and finally executes cb Operationca. setComponent (c); cb. setComponent (ca); cb. operation ();}}

 

Summary:

Decorator mode application scenarios:

1. When you want to add new responsibilities to an object transparently and dynamically.

2. The responsibilities added to an object may be increased or decreased in the future.

3. If the inheritance Extension function is not realistic, you should consider using a combination.

Advantages of the modifier mode:

1. The ability to dynamically expand the functions of objects is realized through combination rather than inheritance.

2. This effectively avoids the flexibility and unrestricted expansion of sub-classes due to the use of inheritance to extend object functions.

3. Make full use of the strengths and weaknesses of inheritance and combination, and find a perfect balance between flexibility and scalability.

4. Though they are of the same type, they are completely independent of each other and can be changed independently.

5. comply with most GRASP principles and common design principles, with high cohesion and low coupling.

Disadvantages of the modifier mode:

1. The decorative chain cannot be too long; otherwise, the efficiency will be affected.

2. Because all objects inherit from Component, if the internal structure of Component changes, it will inevitably affect all sub-classes (decorator and decorator). That is to say, the relationships established through inheritance are always fragile. If the base class changes, it will inevitably affect the interior of the object, and through the combination (Decoator has a Component) the established relationship only affects the external features of the decorated object.

3. Use the modifier mode only when necessary. Otherwise, the complexity of the program will be increased and the system maintenance difficulty will be increased.




 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.