1. Problems arising from succession
Introduction: Said Brother Wai that year entrepreneurial open shop-selling soy milk! Because of pure soy milk, branch almost Bloom County City all village, because the development is too fast, so eager to achieve a computer-managed automated accounting system.
Ingredients: Soy milk
Ingredients: sugar black bean grain eggs ..., add the same ingredients add money, each kind of soy milk prices are different. But they are based on the price of soy milk to increase. Can add a variety of ingredients.
If you use inheritance, each additional ingredient, we will create a lot of new classes, such as black bean grain soy milk, black bean grain sugar soy milk. Will cause the class to explode!
So the way to inherit directly is not possible.
2, decorator design mode.
Intent: To dynamically add some additional responsibilities to an object. For added functionality, the decorator mode is more flexible than generating subclasses. This mode extends the functionality of the object in a transparent manner to the client.
Applicable environment: Add responsibilities to individual objects in a dynamic, transparent manner without affecting other objects. Handle the duties that can be undone. When a method of generating subclasses cannot be used for expansion. 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.
Decorator mode, just create a new ingredient class, and do not need to create all possible subclasses.
Involved roles
Abstract component: Defines an abstract interface to standardize the classes that prepare additional functionality.
Specific component: The class that will be attached to the function, implementing the abstract component role interface.
Abstract Decorator: holds a reference to a specific component role and defines an interface that is consistent with the abstract component role.
Specific decoration: implements the abstract decorator role, which is responsible for adding additional functionality to specific artifacts.
Code implementation:
Drink.java interface of the adorner object
Doujiang.java Concrete Decorator objects,
Decorator.java Decorator base class
Eggdecorator.java specific Decorator object,
Sugardecorator.java specific Decorator object,
Test.java Test
3. Code
Drink Interface Class
Package com.day15;/** * *. The topmost, abstract component, defines an abstract interface * @author Administrator * */public interface Drink {public String description ();p ublic float cost ();
Doujiang class, implementing Drink interface
Package com.day15;/** * Concrete Decorator class * Abstract Adorner interface * @author Administrator * */public class Doujiang implements Drink{private Stri ng name = ""; @Overridepublic String description () {return "pure soy milk";} @Overridepublic float Cost () {return 3f;}}
Decorator Decorator Abstract class
Package com.day15;/** * Decorator Abstract class * @author Administrator * */public class Decorator implements Drink{private Drink drink;//to Decorated object public Decorator (Drink Drink) {this.drink = Drink;} @Overridepublic String Description () {return drink.description ();} @Overridepublic float Cost () {return drink.cost ();}}
Eggdecorator class
Package Com.day15;public class Eggdecorator extends Decorator {public eggdecorator (Drink Drink) {super (Drink);} @Overridepublic String Description () {return super.description () + "+ egg";//note here that the return is Super + ""} @Overridepublic float Co St () {return super.cost () + 1.5f;}}
Sugerdecorator class
Package Com.day15;public class Sugerdecorator extends Decorator {public sugerdecorator (Drink Drink) {super (Drink);// Todo auto-generated constructor stub} @Overridepublic String description () {//Todo auto-generated method Stubreturn Super . Description () + "+ Add Sugar";} @Overridepublic float Cost () {//TODO auto-generated method Stubreturn super.cost () + 0.5f;}}
Test class
Package Com.day15;public class Test {public static void main (string[] args) {Drink Drink = new Doujiang ();//Pure soy milk Sugerdecor Ator Suger = new Sugerdecorator (drink); Add sugar eggdecorator egg = new Eggdecorator (suger);//add sugar and add egg System.out.println (egg.description ()); System.out.println (Egg.cost ());}}
Look, it's convenient.
JAVA---decorator mode