C # learning notes for object-oriented design patterns (9)-decorator decoration patterns (Structural Patterns)

Source: Internet
Author: User

In some cases, we may "over-use inheritance to extend the object's functions". This extension method lacks flexibility due to the static (compile-time) characteristics introduced by inheritance as a type; 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 (more inheritance ).

How can we enable "extension of object functions" to be implemented dynamically (during runtime) 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?

Intention (Intent) 

Dynamically add some additional responsibilities to an object. The decorator mode is more flexible than the subclass generation function.

 

 

  Public     Abstract     Class  Tank
{
Public Abstract Void Shot ();
Public Abstract Void Run ();
}

Public Abstract Class Decorator: Tank // Interface inheritance
{
Private Tank tank; // Has-A Object combination

Public Decorator (tank)
{
This . Tank = Tank;
}

Public Override Void Shot ()
{
Tank. Shot ();
}

Public Override Void Run ()
{
Tank. Run ();
}
}

 

  ///     <Summary>  
/// Decoration Class
/// </Summary>
Public Class Decoratora: decorator
{
Public Decoratora (tank)
: Base (Tank)
{
}

Public Override Void Shot ()
{
// Extended infrared function
// Do shot...
Base . Shot ();
}

Public Override Void Run ()
{
// Extended infrared function
// Do run...
Base . Run ();
}
}

/// <Summary>
/// Decoration Class B
/// </Summary>
Public Class Decoratorb: decorator
{
Public Decoratorb (tank)
: Base (Tank)
{
}

Public Override Void Shot ()
{
// Amphibious Functions
// Do shot...
Base . Shot ();
}

Public Override Void Run ()
{
// Amphibious Functions
// Do run...
Base . Run ();
}
}

/// <Summary>
/// Decoration Class C
/// </Summary>
Public Class Decoratorc: decorator
{
Public Decoratorc (tank)
: Base (Tank)
{
}

Public Override Void Shot ()
{
// Satellite Positioning Function Extension
// Do shot...
Base . Shot ();
}

Public Override Void Run ()
{
// Satellite Positioning Function Extension
// Do run...
Base . Run ();
}
}

 

  Class  App
{
Public Static Void Main ()
{
Tank = New T50 ();
Decoratora da = New Decoratora (tank ); // Extended infrared function
Decoratora DB = New Decoratora (DA ); // Amphibious Functions
Decoratora DC = New Decoratora (db ); // Satellite Positioning Function Extension
}
}

 

Key points:

By using a combination rather than an inheritance method, the decorator mode enables the ability to dynamically expand object functions at runtime, and can expand multiple functions as needed. This avoids the "poor flexibility" and "Multi-subclass Derivative Problems" caused by inheritance ".

The decorator class representsIs-a componentThat is, the decorator class inherits the interfaces of the component class. However, the implementation is as follows:Has-a componentThat is, the decorator class uses another component class.

The decorator mode does not solve the problem of "Multi-subclass-derived multi-inheritance, the main point of the decorator mode application is to solve the problem of "the extension function of the Subject Class in multiple directions", which is the meaning of "decoration.

Applicability 

The decoration mode should be used in the following circumstances:

1. You need to extend the functions of a class or add additional responsibilities to a class.

2. You need to dynamically add functions to an object. These functions can be dynamically revoked.

3. A large number of functions are generated by the arrangement and combination of some basic functions to make the inheritance relationship unrealistic.

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.