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