Design Mode learning notes-decorator decoration Mode

Source: Internet
Author: User

Decorator decoration mode: it is mainly used to dynamically add some additional responsibilities to an object. The extended function is more flexible than the subclass generation method.
First go to our example:
 ProgramFor example


Define abstract base class abscar
Here it represents an abstract "car", which is both the base class of the decoration class and the base class of the decoration class.CodeAs follows:

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Decoratorcar
{
Abstract Class Abscar
{
 
Public   Virtual Void Addaccessary ()
{
Console. writeline (
" The vehicle has standard configuration " );
}
}
}

Define the decorator class. This class inherits from the abscar class and is also the base class of the specific decoration class. Here it represents the concept of "decoration", but it has not been further instantiated as a specific "decoration" content, the Code is as follows:

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Decoratorcar
{
Class Decorator: abscar // Inherited interface (is a abscar)
{
Private Abscar _ car; // Also synthesize abscar (has a abscar)

Public Decorator (abscar car)
{
This . _ Car = CAR;
}
Public   Override   Void Addaccessary ()
{
_ Car. addaccessary ();
}
}
}

Define the specific decoration classes decoratorcruisectrl and decoratorgpsctrl. They inherit from the decorator class and represent the specific "decoration" content. The Code is as follows:

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Decoratorcar
{
Class Decoratorcruisectrl: decorator
{

Private   String _ Cruise;
Public   String Cruise
{
Get { Return _ Cruise ;}
Set {_ Cruise = Value ;}

}
PublicDecoratorcruisectrl (abscar car)
:
Base(CAR)
{}

Public   Override   Void Addaccessary ()
{
// Function Expansion
This . Cruise =   " Add a cruise control system " ;
Console. writeline (
This . Cruise );
Base . Addaccessary ();
}
}
}

 

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Decoratorcar
{
Class Decoratorgpsctrl: decorator
{
Private   String _ GPS;
Public   String GPS
{
Get { Return _ GPS ;}
Set {_ GPS = Value ;}

}
Public Decoratorgpsctrl (abscar car)
:
Base (CAR)
{}

Public   Override   Void Addaccessary ()
{
// Function Expansion
This . GPS =   " Add a GPS Control System " ;
Console. writeline (
This . GPS );
Base . Addaccessary ();
}
}
}

Define the specific object to be decorated: toyotacar. Of course, you can also define other objects to be decorated, such as the horse-drawn carriage. The Code is as follows:

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

NamespaceDecoratorcar
{
ClassToyotacar: abscar
{
PublicToyotacar ()
{

}
Public   Override   Void Addaccessary ()
{
Console. writeline (
" ---------------------------- " );
Console. writeline (
" The above are other functions added to Toyota Motor " );
Base . Addaccessary ();
}
}
}

Application code:

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;

Namespace Decoratorcar
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Toyotacar tcar
=   New Toyotacar ();
Decoratorcruisectrl Dr
=   New Decoratorcruisectrl (tcar );
Decoratorgpsctrl DC
=   New Decoratorgpsctrl (DR );
DC. addaccessary ();

Console. Readline ();

}
}
}

example:
you can understand the above program. In the future, you will need to add a new function. For example, if you want to add a skylight sunroof, only one class that is similar to the decoratorcruisectrl and decoratorgpsctrl functions can inherit the decorator. The most interesting thing about this mode is that the decorator not only inherits the abscar but also integrates the abscar. That is to say, the decorator is a very special class in the decorator mode, it inherits from the abscar [is a relation] and maintains a reference [has a relation] pointing to the abscar instance. In other words, there is a dynamic combination between decorator and abscar, there is also a static inheritance relationship.
1. Why is this design? We know that the advantage of a combination is that it can add responsibilities to the object at runtime. The goal of decorator [has a] abscar is to allow the decorator to dynamically add responsibilities to the abscar at runtime, this is relatively easy to understand.
2. What is the purpose of decorator inherited from abscar? There is only one purpose, that is, to unify decoratorcruisectrl, decoratorgpsctrl, or any other specific decorator) and decorator (Toyota car or any other brand car). In other words, no matter what the decoratorcruisectrl or decoratorgpsctrl is, they all inherit from the top-level abscar base class, user code can treat them as abscar. The deeper benefit of this is that the decoratorcruisectrl object (decoratorgpsctrl) is the decoratorgpsctrl object (fengtian CHE) function responsibility extension is completely transparent to the user code, because the user code references abscar, so it is not because the "decorated by" (Toyota car) object is decorated, an error occurs in the user code that references it. In fact, there is no impact, because the user code references abscar objects before and after the decoration.
therefore, the "decoration mode" unifies the interfaces of the "decoration" and "decorated" through inheritance, the ability to dynamically expand the decorator object at runtime is achieved through combination.
program running effect:

Summary:
Decorator pattern:
You have an existing component class, but you cannot inherit it (subclassing ).
Dynamically add roles (adding statuses and actions) to objects ).
Changes the members and behaviors of a class, but does not affect other objects.
It is hoped that the responsibilities can be easily revoked.
You do not want to use inheritance to expand behavior. One of the reasons is to avoid the use of "inheritance" Alone when some features need to be referenced in combination with each other, which will produce too many subclass and too complex class graph structures, another consideration may be that the class definition is hidden, or the class definition cannot be used to generate a subclass.

Advantages of decorator pattern:
This prevents the use of inheritance alone from being elastically scalable and possibly deriving too many child classes.
You do not need to modify existing code during expansion.
You can dynamically add new behaviors (Responsibilities) during the execution period ).

Disadvantages of decorator pattern:
Many small classes may appear in the program, that is, many concretedecorator classes need to be compiled (Specific decorator ).
Excessive use of the decorator mode will complicate the program logic.
It is difficult for others to understand the design method and code, and to troubleshoot, track, and debug faults.

Other features of decorator pattern:
Each function to be decorated is placed in a separate class.
We can wrap a component with countless decorators.
The decorator can add its own behavior before or after the decorator, and even replace the behavior of the decorator for a specific purpose.
The decorator does not need to know that it has been "decorated", that is, the component class (object) does not need to know the existence of the decorator class (object, in addition, decorator only knows component.

Go to: Design Mode Study Notes List

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.