An introduction to the Decorator pattern for Java design patterns

  • 2020-04-01 03:41:43
  • OfStack

Decorator is often translated as "decoration". I think it is more visual to translate as "painter". Decorators are used for painting. These two entities are required in the Decorator pattern.

Decorator definition: dynamically adding some additional responsibilities to an object, like painting a wall. Using the Decorator pattern is more flexible than subclassing the functionality.

Why use decorators

We can often use inheritance to realize the function, if the need to expand the function of the type is various, so is bound to generate a lot of children, increase the complexity of the system, at the same time, using inheritance function expansion, we must be predictable the expanding function, the function is to compile time to determine, is static.

The reason for using decorators is that these features need to be dynamically determined by the user as to how and when to join. Decorator provides a "plug-and-play" way to decide at run time when and what features to add.

How to use decorative patterns

For example, there are two kinds of piling in the Adapter: square pile and round pile. The Adapter pattern shows how to combine the two classes. In the Decorator pattern, we are going to add some extra functions when piling, such as digging holes and driving boards on the pile, without caring about how to use two unrelated classes.

Let's first establish an interface:


public interface Work{
 public void insert();
}

The interface Work has a concrete implementation: insert square or circular piles, both of which are fine for decorators. We take inserting square pile as an example:

public class SquarePeg implements Work{
 public void insert(){
  System.out.println(" Square pile insertion ");
 }
}

Now there is an application: the need to dig a hole before the pile is driven in, and after the pile is driven in, the additional function is dynamic, may be added at will to adjust the modification, for example, may need to nail the frame after the pile is driven (just a metaphor).

So we use the Decorator pattern, where the SquarePeg SquarePeg is the decoratee, and we need some "paint" on the decoratee, and that paint is those extra features.


public class Decorator implements Work{
 private Work work;
 //The additional functionality is packaged in this List
 private ArrayList others = new ArrayList();
 //Introduce the Work object in the constructor by combining the new method. < br / >  public Decorator(Work work){
  this.work=work;
  others.add(" Dig a hole ");
  others.add(" Pound boards ");
 }
 public void insert(){
  newMethod();
 }
 //In the new method, we add other methods before the insert, in this case in the order of
specified by the user  public void newMethod(){
  otherMethod();
  work.insert();
 }
 public void otherMethod(){
  ListIterator listIterator = others.listIterator();
  while (listIterator.hasNext()){
   System.out.println(((String)(listIterator.next())) + " The ongoing ");
  }
 }
}

In the above example, we placed the digging and nailing boards ahead of the piling insert, just to illustrate that the order of additional functions can be arbitrary.

Okay, the Decorator pattern is up, let's see how to call:


Work squarePeg = new SquarePeg();
Work decorator = new Decorator(squarePeg);
decorator.insert();

The Decorator pattern is now complete.

If you look carefully, the call above is similar to the call we made when reading the file:


    FileReader fr = new FileReader(filename);
    BufferedReader br = new BufferedReader(fr);

In fact, Java's I/O API is implemented using Decorator. There are many variations of I/O, and if they all take the inheritance method, they will produce many subclasses, which is obviously quite tedious.

The Decorator implementation in Jive

In the forum system, some special words are not allowed to appear in the forum such as "down with XXX", we need to filter these" reactionary "fonts. Do not allow them to appear or high brightness display.

In the IBM Java column devoted to Jive, forummessagefilter. Java USES the Decorator pattern, but the program doesn't actually use the Decorator. Instead, it prompts that you can reorganize ForumMessageFilter as the Decorator pattern by designing additional filtering capabilities for specific forums.

So, while we're trying to figure out whether the Decorator pattern is really there and will actually be used, it's important to understand the definition of the Decorator pattern and the roles involved in it (Decoratee and Decorator).


Related articles: