Decorator is often translated as "decoration". I think it is more vivid to translate it into "painter". The painter (decorator) is used to paint, so we call the object of painted decoratee. These two entities are required in the Decorator mode.
Decorator definition: dynamically adds some extra responsibilities to an object, like painting the wall. Using the Decorator mode is more flexible than using the generation of subclasses to achieve functional expansion.
Why use Decorator
We can usually use inheritance to achieve function expansion. If there are many types of functions that need to be expanded, many subclasses will inevitably be generated to increase the complexity of the system. At the same time, use inheritance to achieve function expansion, we must foresee these expansion functions. , these functions are determined at compile time and are static.
The reason for using Decorator is that these functions need to be decided dynamically by the user to join. Decorator provides a "plug and play" method to decide when to add what features during runtime.
How to use decorative mode
Take the example of pile driving in Adapter. There are two types of piles in Adapter: square piles and circular piles. The Adapter mode shows how to use these two classes in combination. In the Decorator mode, we want to add some extra functions when driving piles, such as digging. The pit is nailed on the pile, etc., and it doesn't care how to use two unrelated classes.
Let's create an interface first:
The code copy is as follows:
public interface Work{
public void insert();
}
Interface Work has a specific implementation: insert square piles or circular piles. These two differences do not matter to the Decorator. Let's take the insertion of a square pile as an example:
The code copy is as follows:
public class SquarePeg implements Work{
public void insert(){
System.out.println("Square pile insertion");
}
}
There is now an application: before the pile is driven, digging a hole, and after the pile is driven, the wooden board is nailed on the pile. These additional functions are dynamic, and adjustments may be added at will. For example, it may be necessary to nail the rack after the pile is driven ( Just a metaphor).
Then we use the Decorator mode, where the square pile SquarePeg is the decoratee (painted by paint), we need to paint some "paint" on the decoratee, and these paints are those extra functions.
The code copy is as follows:
public class Decorator implements Work{
private Work work;
//The additional functionality is packaged in this List
private ArrayList others = new ArrayList();
//Use the combined new method in the constructor to introduce Work objects;
public Decorator(Work work){
this.work=work;
others.add("Dig a hole");
others.add("nail board");
}
public void insert(){
newMethod();
}
//In the new method, we add other methods before insert, the order here is specified by the user flexibly
public void newMethod(){
otherMethod();
work.insert();
}
public void otherMethod(){
ListIterator listIterator = others.listIterator();
while (listIterator.hasNext()){
System.out.println(((String)(listIterator.next())) + "In progress");
}
}
}
In the above example, we ranked both the digging holes and nailing boards in front of the pile insert. Here we are just an example to illustrate that the order of additional functions can be arranged at will.
OK, the Decorator mode is out, let's see how to call it:
The code copy is as follows:
Work squarePeg = new SquarePeg();
Work decorator = new Decorator(squarePeg);
decorator.insert();
Decorator mode is completed here.
If you are careful, you will find that the above call is similar to the call we read when we read the file:
The code copy is as follows:
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
In fact, Java's I/O API is implemented using Decorator. There are many I/O variants. If all inheritance methods are adopted, many subclasses will be generated, which is obviously quite cumbersome.
Decorator implementation in Jive
In the forum system, some special characters cannot appear in the forum, such as "Down with XXX", we need to filter these "reactionary" fonts. Don't let them appear or display them in high brightness.
In an article specifically talking about Jive in the IBM Java column, it was mentioned that ForumMessageFilter.java in Jive uses the Decorator mode. In fact, the program does not really use Decorator, but prompts that additional filtering functions can be designed for special forums , then you can reorganize ForumMessageFilter as the Decorator mode.
Therefore, when we are deciding whether it is really the Decorator mode and will really use the Decorator mode, we must grasp the definition of the Decorator mode and the roles involved (Decoratee and Decorator).