I. Background: Decorator
* Decorator is often translated into "decoration". I think decorator is used to paint a painter, which is called decoratee. these two entities are required in the Decorator mode.
* Decorator definition:
Dynamically add some additional responsibilities to an object, just like painting on the wall. Using the Decorator mode is more flexible than using the subclass method to Expand functions.
* Why is Decorator used?
We can usually use inheritance to Expand functions. If there are many types of functions to be extended, many sub-classes will be generated to increase the complexity of the system. At the same time, when using inheritance to implement function expansion, we must be able to foresee these extended functions, which are determined at compilation and static.
The reason for using Decorator is: These functions need to be dynamically determined by the user to join the method and timing. Decorator provides the "Plug and Play" method, during which to decide when to add the function.
* This mode is initially summarized
1. interfaces for basic functions
2. the Decorator parameter is the interface itself and also the interface so as to be the parameter of the next Decorator
3. The basic function class implements the interface and serves as the parameter of the Decorator constructor, so that new functions can be added on this basis.
4. The extra functions are processed by the data structure in the Decorator.
Ii. Problem
This is an example of the Decorator design pattern:
Basic functions: Counter class
Features to be added
1: Upper Limit Control
2: lower limit control
Import java. io .*;
Class Counter {
Private int value;
Public Counter (int v ){
System. out. println ("init me here in The Counter with value! ");
Value = v;
}
Public Counter (Counter cc ){
System. out. println ("init me here in The Counter with class! ");
Value = cc. value;
}
Public int read_value (){
System. out. println ("read me here The value is:" + value );
System. out. println ("read me here in The Counter! ");
Return value;
}
Public void increment (){
System. out. println ("increment me here in The Counter! ");
Value ++;
}
Public void decrement (){
System. out. println ("decrement me here in The Counter! ");
Value --;
}
}
Class Decorator extends Counter
{
Counter counter;
Public Decorator (Counter c)
{
Super (c );
System. out. println ("init me here with class Decorator! ");
}
}
Class UpperLimit extends Decorator // upper limit control
{
Public UpperLimit (Counter c)
{
Super (c );
Counter = c;
System. out. println ("init me here with class UpperLimit! ");
}
Public void increment ()
{
If (counter. read_value ()> 20)
{
System. out. println ("Too High ");
}
Else
{
System. out. println ("increment me here with class UpperLimit! ");
Counter. increment ();
}
}
/* Public void decrement ()
{
Counter. decrement ();
}
Public int read_value ()
{
Return counter. read_value ();
}*/
}
Class LowerLimit extends Decorator // lower limit control
{
Public LowerLimit (Counter c)
{
Super (c );
Counter = c;
System. out. println ("init me here in The Counter with class LowerLimit! ");
}
Public void decrement ()
{
System. out. println ("Class value:" + read_value ());
System. out. println ("Dec value:" + counter. read_value ());
If (counter. read_value () <= 0)
{
System. out. println (counter. read_value ());
System. out. println ("Too Low ");
}
Else
{
System. out. println ("decrement me here in The Counter with class LowerLimit! ");
Counter. decrement ();
}
}
/* Public void increment ()
{
Counter. increment ();
}
Public int read_value ()
{
Return counter. read_value ();
}*/
}
Class CounterFactory
{
Public static Counter createCounter (int value, int op)
{
Switch (op)
{
Case 1:
{
Return new Counter (value );
}
Case 2:
{
Return new UpperLimit (new Counter (value ));
}
Case 3:
{
Return new LowerLimit (new Counter (value ));
}
Default:
{
Return new UpperLimit (new LowerLimit (new Counter (value )));
}
}