Java Decorator Pattern (Understanding code principles from a real-life perspective) _java

Source: Internet
Author: User
Tags abstract class definition

The decorator pattern can dynamically add some additional responsibilities to an object. Decorator mode is more flexible than generating subclasses in terms of adding functionality.

The applicable environment for this mode is:

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

(2) Dealing with duties that can be undone.

(3) When the method of generating subclasses cannot be extended. One scenario is that there may be a large number of independent extensions that will produce a large number of subclasses to support each combination, making the number of subclasses explode. Another scenario might be because the class definition is hidden, or the class definition cannot be used to generate subclasses.

Key steps to implement this pattern:

(1) Component ( decorated object base class ): Defines the interface of objects that can dynamically add responsibilities to these objects;

(2) Concretecomponent (specifically decorated object): Define specific objects, decorator can give it additional responsibilities;

(3) Decorator (Decorator abstract Class): maintains a reference to a component instance, defining an interface that is consistent with component (that is, to inherit or implement the base class of the Decorated object );

(4) Concretedecorator (specific decoration): Specific decorative objects, to the internal holding of specific decorative objects to add specific responsibilities;

This may be a little difficult to understand, then we still the old rules quiet:

Winter after the weather is getting colder, after work, as a senior cargo, about two or three friends happy to have a hot pot feast again. Speaking of hot pot, have to mention in Chengdu to eat the Big Dragon 燚 hotpot, all kinds of pot, with everything, but my favorite is the big Dragon 燚 hotpot flavor pot, spicy beef, broadsword hair belly, sausage, mound of beef, spicy ribs and so on, think about drooling ah.

Said this everyone combined with the decoration of the implementation steps, it should feel a little bit, the above mentioned bottom of the pot, in fact, is decorated with the object of the base class , the ingredients are actually decorative abstract class , Tai Lung 燚 hotpot Flavor pot of the original pot is specific to the bottom of the pan is specifically decorated with , Broadsword hair belly, day taste sausage, mound of beef, spicy pork ribs decorated with the bottom of the pot with a variety of dishes that are specific decorative objects . Say this, everyone should be enlightened, let's start the specific code implementation:

First step: Define the base class of the decorated object (can be abstract class or interface)

 Public interface Guodi {public
   float (); the bottom of the pot, of course, has a price. Public
   String name ();
 

Step two: Define the objects that are specifically decorated (that is, the bottom of the pot, which is defined here)

public class Yuanyang implements Guodi {
  @Override public
  float () {return
    48.0f;
  }
  @Override public
  String name () {return
    "Mandarin duck pot";
  }
}
public class Dalongyan implements guodi{
  @Override the public
  float cost () {return
    59.0f;
  }
  @Override public
  String name () {return
    "Tai Lung 燚 hotpot plain pot";
  }

Step three: Define the adorner abstract class

Public abstract class Peicai implements Guodi {
  private guodi guodi;
  Public Fooddecorator (Guodi guodi) {
    super ();
    This.guodi = Guodi;
  }
  @Override public
  float () {return
    guodi.cost ();
  }
  @Override public
  String name () {return
    guodi.name ();
  }
}

Step Fourth: Define the specific Adorner object

public class Malaniurou extends Peicai {public
  Malaniurou (Guodi guodi) {
    super (Guodi);
  }
  @Override public
  float () {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 () {return
    super.cost () +30f
  }
  @Override public
  String name () {return
    super.name () + "+ broadsword belly";
  }
}

Test class:

public class Test {public
  static void Main (string[] args) {
    Guodi Guodi = new Dalongyan ()//Point a big dragon 燚 hotpot Flavor Bottom Pot
    MaLa Niurou y = new Malaniurou (guodi);/to a spicy beef
    maodu x = new Maodu (y);//on the basis of spicy beef and then a big knife belly
    System.out.println ("A total point" + X.name () + ", Total consumption" +s.cost ());
  }

Output results:

11 A total of the Tai long 燚 hotpot flavor pot of the original pot + spicy beef + broadsword belly, a total consumption of 135

The above is a small set to introduce the Java decorator Model (from the real-life understanding of code principles), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.