JAVA decorator pattern (understanding code principles from a real life perspective)

  • 2020-05-19 04:46:39
  • OfStack

The decorator pattern dynamically adds some additional responsibilities to an object. In terms of adding functionality, the Decorator mode is more flexible than subclassing.

The applicable environment of this model is:

(1) add responsibilities to a single object in a dynamic and transparent manner without affecting other objects.

(2) deal with responsibilities that can be undone.

(3) when the method of subclass generation cannot be used for expansion. In one case, there may be a large number of independent extensions, and to support each combination will generate a large number of subclasses, making the number of subclasses explode. Another situation might be because the class definition is hidden, or because the class definition cannot be used to subclass.

Key steps to implement this pattern:

(1) Component (base class of decorated objects) : define the interface of objects, which can dynamically add responsibilities to these objects;

(2) ConcreteComponent (specific decorated object) : define a specific object, and Decorator can add additional responsibilities to it;

(3) Decorator (decorator abstract class) : maintain the reference to the instance of Component, define the interface to Component1 (that is, inherit or implement the base class of the decorated object);

(4) ConcreteDecorator (specific decorator) : specific decoration objects, adding specific responsibilities to the specific objects to be decorated held inside;

It may be a little hard for you to understand this, but let's be quiet as usual:

After entering the winter weather more and more cold, after work, as a senior food, about 23 friends to a hot pot feast again cool. Speaking of hot pot, I have to mention the dalong hot pot I have eaten in chengdu. There are all kinds of dishes on the bottom of the pot, but my favorite ones are the plain bottom of the dalong hot pot, spicy beef, broadsword maowu, tianwei sausage, jucuaju beef, and spicy spare ribs, which makes my mouth water.

Said that everybody combining with the implementation steps of decorator, should have a little feeling, above the bottom of the pot, is the base class of the object being decorated, ingredients abstract class is a decorator, dragon � chafing dish flavor pot these specific bottom of concrete was decorative objects, spicy beef, sword belly, day flavor sausage, MAO tuo tuo meat, spicy ribs these decorative POTS with a variety of dishes is the adornment of the specific object. Speaking of which, everyone should be suddenly enlightened, let's start the specific code implementation:

Step 1: define the base class of the decorated object (which can be an abstract class or an interface)


 public interface GuoDi {
   public float cost();// Of course there's a price for the bottom 
   public String name();// You have to have a name 
 }

Step 2: define the specific objects to be decorated (i.e., the POTS, here define two)


public class YuanYang implements GuoDi {
  @Override
  public float cost() {
    return 48.0f;
  }
  @Override
  public String name() {
    return " Yuanyang pot ";
  }
}
public class DaLongYan implements GuoDi{
  @Override
  public float cost() {
    return 59.0f;
  }
  @Override
  public String name() {
    return " Dalong and authentic hot pot ";
  }
}

Step 3: define the decorator abstract class


public abstract class PeiCai implements GuoDi {
  private GuoDi guodi;
  public FoodDecorator(GuoDi guodi) {
    super();
    this.guodi = guodi;
  }
  @Override
  public float cost() {
    return guodi.cost();
  }
  @Override
  public String name() {
    return guodi.name();
  }
}

Step 4: define the concrete decorator object


public class MaLaNiuRou extends PeiCai {
  public MaLaNiuRou(GuoDi guodi) {
    super(guodi);
  }
  @Override
  public float cost() {
    return super.cost()+46f;
  }
  @Override
  public String name() {
    return super.name()+"+ Spicy beef ";
  }
}
public class MaoDu extends PeiCai {

  public MaoDu(GuoDi guodi) {
    super(guodi);
  }
  @Override
  public float cost() {
    return super.cost()+30f;
  }
  @Override
  public String name() {
    return super.name()+"+ Broadsword MAO belly ";
  }
}

The test class:


public class Test {
  public static void main(String[] args) {
    GuoDi guodi = new DaLongYan ();// Order a dalong and taste of hot pot 
    MaLaNiuRou y = new MaLaNiuRou(guodi);// I'll have the spicy beef 
    MaoDu x = new MaoDu(y);// On the basis of spicy beef to a big knife MAO tripe 
    System.out.println("1 The concurrent "+x.name()+", Total consumption "+s.cost());
  }
}

Output results:

1. Ordered a total of 135 dishes, including dalong and chaffy flavor, spicy beef and broadsword maodu


Related articles: