Design Pattern-decorator patterns)

Source: Internet
Author: User

Design Mode(9): decorator patterns)

 

Decorator patterns)

Definition

 By dynamically adding some additional responsibilities to an object, the decoration mode is more flexible than the subclass generation.

 

Overview

In software systems, we sometimes use inheritance to extend the functions of objects. However, due to the static characteristics introduced by inheritance as types, this expansion method lacks flexibility; as the number of child classes increases (the number of extended functions), the combination of various child classes (the combination of extended functions) will lead to the expansion of more child classes. How can we enable "Object Function Extension" to be dynamically implemented as needed? At the same time, avoid the subclass expansion problem caused by "more extended functions? So that any "function scaling changes" will cause the lowest impact? This is what we will talk about Decorator Mode. Dynamically add some additional responsibilities to an object. To add a function, Decorator The mode is more flexible than the subclass generation.

decorator patterns) structure chart >

Defines a specific class, or you can add some responsibilities to this object. Decorator: an abstract decorative class that inherits component's function of extending the component class from the external class. However, for component, you do not need to know the existence of the decorator. As for concretedecorator, It is a specific decoration object and serves to add responsibilities to component. 

Abstract   Class Component
{
Public   Abstract   Void Operation ();
}

ClassConcretecomponent: Component
{
Public Override VoidOperation ()
{
Console. writeline ("Operations on specific objects");
}
}

Class Decorator: Component
{
Protected Component component;
Public   Void Setcomponent (Component component) // Set component
{
This . Component = Component;
}
Public   Override   Void Operation ()
{ // Override operation (). The actual execution is the operation () of component ()
If (Compoonent ! = Null )
{
Component. Operation ();
}
}
}

Class Concretedecpratora: decorator
{
Private   String Addedstate; // Unique functions of this class to distinguish concretebecoratorb
Public Vorride Void Operation ()
{
// First run the operation () of the original component, and then execute the functions of this class, such as addedstate, which is equivalent to decorating the original component.
Base . Operation ();
Addedstate = " New State " ;
Console. writeline ( " Operations of decoration object " );
}
}

Class Concretedecpratora: decorator
{
Public   Override   Void Operation ()
{
// First run the operation () of the original component, and then execute the functions of this class, such as addedbehavior (), which is equivalent to decorating the original component.
Base . Operation ();
Addedbehavior ();
Console. writeline ( " Operations on decoration object B " );
}

Private   Void Addedbehavior () // Unique functions of this class to distinguish concretebecoratora
{
}
}

Static   Void Main ( String [] ARGs)
{
Concretecomponent C = New Concretecomponent ();
Concretedecoratora d1 = New Concretedecoratora ();
Concretedecoratorb D2 = New Concretedecoratorb ();

// The decoration method is as follows: first, use concretecomponent to instantiate Object C, and then use concretedecoratora's instantiated object D1 to Package C,
Use the concretedecoratorb object D2 to package D1, and finally execute the D2 operation ()
D1.setcomponent (C );
D2.setcomponent (D1 );
D2.operation ();
}

 

Summary

In the decoration mode, each function to be decorated is placed in a separate class and the class is used to wrap the object to be decorated. Therefore, when special behavior is required, the customerCodeYou can use the decoration function to package objects in sequence as needed.

 

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.