Design Mode note-decoration Mode

Source: Internet
Author: User

Definition:

The decoration mode dynamically extends the functions of an object without changing the original class file and using inheritance. It creates a packaging object, that is, decoration, to package a real object.

Features:

(1) The decoration object has the same interface as the real object. In this way, the client object can interact with the decoration object in the same way as the real object.

(2) A decoration object contains a reference of a real object)

(3) The decoration object accepts all requests from the client. It forwards these requests to real objects.

(4) Decoration objects can add some additional functions before or after forwarding these requests. In this way, you can add additional functions externally without modifying the structure of the given object during running. In the object-oriented design, the function of a given class is extended by inheritance.

 

Advantages:

· More flexible than inheritance
From the perspective of adding functions to objects, the decoration mode is more flexible than inheritance. Inheritance is static, and all child classes have the same function. However, the decoration mode separates functions into each decorator, and dynamically combines functions at runtime by means of object combination. What functions does each decorated object ultimately have, it is determined by the dynamic combination of functions during the runtime.

· Easier function Reuse
The decoration mode disperses a series of complex functions into each decorator. Generally, a decorator only implements one function, which makes it easy to implement the decorator, more importantly, this is conducive to the reuse of the decoration device function. You can add multiple identical decoration devices to an object, or use a decoration device to decorate different objects, in this way, the function of the decorator is reused.

· Simplified high-level Definition
The decoration mode can add any number of functions to the object by combining the decorator. Therefore, you do not need to define all the functions in the upper-level definition, instead, you can define the most basic functions. when you need them, you can combine the corresponding decorators to complete the required functions.

Disadvantages:

· Will produce many fine-grained objects
As mentioned above, the decoration mode is to distribute a series of complex functions into each decoration device. Generally, a decoration device only implements one function, which produces many fine-grained objects, the more complex the function is, the more fine-grained objects are required.

Use Cases:

1. When we need to dynamically Add a new function or responsibility for an existing object, we can consider using the decoration mode.
2. when the responsibilities of an object change frequently or you need to dynamically add roles, you can avoid extending the inheritance subclass to adapt to such changes, this method causes the subclass to expand too quickly and is difficult to control.

 

Display Column

 

[Java]
1. object interface Component. cs
Public abstract class Component
{
Public abstract void Operation ();
}

2. The ConcreteComponent. cs implementation class inherits from the Component interface.
Public class ConcreteComponent: Component
{
Public override void Operation ()
{
Console. WriteLine ("starting to execute a specific object ...");
}
}

3. Decorator abstract class. cs inherits from the Component interface.
Public abstract class Decorator: Component
{
Private Component m_Component;

Public void SetComponent (Component component)
{
This. m_Component = component;
}

Public override void Operation ()
{
If (m_Component! = Null)
{
M_Component.Operation ();
}
}
}

4. The specific decoration object ConcreteDecoratorA. cs inherits from the Decorator abstract class.
Public class ConcreteDecoratorA: Decorator
{
Private string addedState;
Public override void Operation ()
{
Base. Operation ();
AddedState = "state attribute decoration. ";
Console. WriteLine (addedState );
}
}
Added the state attribute addedState to the object interface Component.

5. The specific decoration object ConcreteDecoratorB. cs inherits from the Decorator abstract class.
Public class ConcreteDecoratorB: Decorator
{
Public override void Operation ()
{
Base. Operation ();
AddedBehavior ();
}

Private void AddedBehavior ()
{
Console. WriteLine ("decorated with Operation behaviors. ");
}
}


Added the operation behavior AddedBehavior () to the object interface Component ()

5. client code
Static void Main (string [] args)
{
ConcreteComponent cc = new ConcreteComponent ();
ConcreteDecoratorA cda = new ConcreteDecoratorA ();
ConcreteDecoratorB cdb = new ConcreteDecoratorB ();

Cda. SetComponent (cc );
Cdb. SetComponent (cda );
Cdb. Operation ();
Console. Read ();
}

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.