Decorator mode can dynamically add some additional responsibilities to an object. For added functionality, the decorator mode is more flexible than generating subclasses.
The applicable environment for this mode is:
(1) Add responsibilities to a single object in a dynamic, transparent manner without affecting other objects.
(2) to deal with duties that can be undone.
(3) When a method of generating subclasses cannot be used to augment. 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, resulting in an explosive increase in the number of subclasses. Another situation may 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 (Base Object): Defines the interface of the object, which can dynamically add responsibilities to these objects;
(2) Concretecomponent (specifically decorated object): Define the specific object, decorator can give it additional responsibilities;
(3) Decorator (Decorator abstract Class): Maintain a reference to a component instance, define an interface that is consistent with component (that is, to inherit or implement the base class of the object being adorned );
(4) Concretedecorator (concrete decorator): Specific decorative objects, to the internal holding of the specific decoration objects to add specific responsibilities;
It may be a bit difficult for us to understand, so we still have the following:
Winter after the weather is getting colder, after work, as a senior foodie, about two or three friends happy to a hot pot feast again cool but. Speaking of hot pot, have to mention in Chengdu to eat the big dragon Brigitte Hotpot , all kinds of pot, with all the dishes, but my favorite is the big dragon Brigitte Hotpot flavor pot, spicy beef, broadsword hair belly, day flavor sausage, Mound mound beef, spicy ribs, etc., think of all drooling ah.
Said this everyone combines the implementation steps of the decorator, should feel a little bit, the above mentioned pot , in fact, is the base of the decorative objects , ingredients are actually decorators abstract class , Tai long Brigitte Hot pot flavor pot These specific bottom of the pot is specifically decorated objects , spicy beef, broadsword hair belly, tian taste sausage, Mound mound beef, spicy ribs These decorative pot with a variety of dishes that are specific decorative objects . say This, everyone should be enlightened, the following we begin the specific code implementation:
First step: Define the base class of the object being decorated (can be an abstract class or an interface)
1 Public Interface Guodi {2 Public float cost (); // The bottom of the pot is a price. 3 Public String name (); // you have to have a name. 4 }
Step two: Define the specific objects to be decorated (i.e., a variety of pots, defined here)
1 Public classYuanyangImplementsGuodi {2 @Override3 Public floatCost () {4 return48.0f;5 }6 @Override7 PublicString name () {8 return"The pot of Mandarin duck";9 }Ten } One Public classDalongyanImplementsguodi{ A @Override - Public floatCost () { - return59.0f; the } - @Override - PublicString name () { - return"Tai Lung Brigitte Hot pot Flavor Pot"; + } -}
Step three: Define the adorner abstract class
1 Public Abstract classPeicaiImplementsGuodi {2 PrivateGuodi Guodi;3 Publicfooddecorator (Guodi guodi) {4 Super();5 This. Guodi =Guodi;6 }7 @Override8 Public floatCost () {9 returnfood.cost ();Ten } One @Override A PublicString name () { - returnfood.name (); - } the}
Fourth step: Define the specific Adorner object
1 Public classMalaniurouextendsPeicai {2 PublicMalaniurou (Guodi guodi) {3 Super(Guodi);4 }5 @Override6 Public floatCost () {7 return Super. Cost () +46f;8 }9 @OverrideTen PublicString name () { One return Super. Name () + "+ spicy Beef"; A } - } - Public classMaoduextendsPeicai { the - PublicMaodu (Guodi guodi) { - Super(Guodi); - } + @Override - Public floatCost () { + return Super. Cost () +30f; A } at @Override - PublicString name () { - return Super. Name () + "+ Broadsword Hairy belly"; - } -}
Test class:
1 Public classTest {2 Public Static voidMain (string[] args) {3Guodi Guodi =NewDalongyan ();//Point a big dragon Brigitte Hotpot original pot4Malaniurou y =NewMalaniurou (Guodi);//have a spicy beef.5Maodu x =NewMaodu (y);//On the basis of spicy beef and then a machete belly6SYSTEM.OUT.PRINTLN ("Total Point" +x.name () + ", consumption" +s.cost ());7 }8}
Output Result:
1 Altogether point a big dragon Brigitte Hotpot original pot + spicy beef + broadsword fur belly, total consumption 135
Do not know everyone this meal satisfied not, do not, we go to Chengdu to this real decorative design mode, see I do not put my big dragon Brigitte Hotpot original pot Decorate let you walk not move road.
Java Decorator Mode (Understanding code principles from a real-life perspective)