Decorator-decorator Mode

Source: Internet
Author: User
Using  System;
Using System. text;

Using System. Collections. Generic;
Using System. Data. LINQ;
Using System. Data. LINQ. sqlclient;

Namespace Test
{
Class Clock // The base class. All classes derived from this class must have the get method.
{
Public Virtual String Get ()
{
Return " The time is " ;
}
}

Class Clockdecorator: clock // decoration class used to decorate the base class and its derived class
{
Private Clock _ clock;
Public Clockdecorator (Clock)
{
This . _ Clock = Clock;
}

Public Override String Get ()
{
Return This . _ Clock. Get ();
}
}

Class Addstring: clockdecorator // The decorator subclass is used to add new responsibilities to the base class or its derived class.
{
Public Addstring (Clock ): Base (Clock)
{

}

Public Override String Get ()
{
String S = base. Get ();
Return S + "" + datetime. Now. tostring ();
}
}

Class Program
{
Public Static Void Main ()
{
Clock C = New Clock ();
Clockdecorator CD = New Addstring (C );
Console. writeline (CD. Get ());
}
}
}

The code written above is simply used to implement the "decoration" of clock class objects ".

The so-called decoration is to add some duties and functions to the class externally without changing the original class. You can create a number of sub-classes of the decorator to decorate the base class. Instead of inheriting sub-classes, you can avoid too many sub-classes.CodeIt may have some ambiguity for "effectively avoiding subclass", so we can expand it as follows:

 Using  System;
Using System. text;

Namespace Test
{
Abstract Class Clockbase
{
Public Abstract String Get ();
}

Class Clock: clockbase
{
Public Override String Get ()
{
Return " The time is " ;
}
}

Class Clockdecorator: clockbase
{
Private Clock _ clock;
Public Clockdecorator (Clock)
{
This . _ Clock = Clock;
}

Public Override String Get ()
{
Return This . _ Clock. Get ();
}
}

Class Addstring: clockdecorator
{
Public Addstring (Clock ): Base (Clock)
{

}

Public Override String Get ()
{
String S = Base . Get ();
Return S + " " + Datetime. Now. tostring ();
}
}

Class Program
{
Public Static Void Main ()
{
Clock C = New Clock ();
Clockdecorator CD = New Addstring (C );
Console. writeline (CD. Get ());
}
}
}

Here, clock and clockdecorator have a common base class clockbase, and there is a total of abstract methods that must be implemented.ProgramIn, instantiate a class clock to add object responsibilities, define a decorator object, and instantiate this object using the decorator subclass. The decorator subclass has a constructor, and add the clock instance object to the constructor. At this time, we can say that the CD object, to a certain extent, is the c object after being decorated. Of course, if the object identification is used to determine the object division, the CD object is not a C object, although they have a common basic class and method.

Run the above Code and you will see that the output is not included in the clock, that is, the specific time is only available after the decoration.

Of course, inheritance can be used to expand the object functions, but consider the following. A base class has many subclasses. If you want to add one or more rarely used features to each subclass, then, we need to generate sub-classes of many sub-classes. If the decorator mode is used, although it has many sub-classes, I can add more than one function to this object as long as I wrap the generated object, however, the written subclass does greatly reduce the subclass to be generated.

All of the above are the advantages of the decorator, which can reduce the generation of sub-classes, and the sub-class of the decorator used to expand the function, can be very specific in charge of a responsibility, but to add a few responsibilities, then you can use the Child class of the single-function decorator to decorate the objects to be decorated. In addition, you can add some other functions before and after executing the original object, such as logging, system status output, and output prompt value. Because the sub-classes of the decorator are derived from the decorator class, you can nest the corresponding methods to perform certain flow operations.

Decorator mode disadvantages. Everything has two sides, so there is something bad if there is good. The main drawback is that this object is a non-peer object, that is, the CD and C in the above example. The root-cause correction is not exactly the same object identifier! Second, the corresponding objects will be generated in the instantiation of the decorator subclass, which will lead to more responsibilities to be added. The more the decorator subclass objects to be generated, the more difficult to control.

Therefore, in any mode, the truth is far from enough. For example, if a function is rarely used, but the class itself lacks this function, these functions do not depend on the class itself, if a subclass is to be derived, it will be messy and inappropriate, causing maintenance difficulties and violating the single responsibility principle, so as far as I am concerned, I will consider using this mode to appropriately expand the role of the object.

If you have any errors or errors, Please comment and criticize them.

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.