When you see the words, you should have a number in your mind. The so-called decoration is to add things and costumes on the basis of the original, such as eyelashes, lipsticks, and fashionable clothes, then it becomes beautiful. For example, a gift box should be added for gifts, and the grade should be improved. You can open a good price, which is also a "decoration ". The modifier mode has a principle to be followed: the open and closed principle, that is, the modification is closed, and the extension is open. Under what circumstances do I need to use the decoration installer mode? My understanding is: When I have provided some methods, and you want to use my method, you can only without damaging my method, it is extended based on my method, that is, overwriting. What are the benefits of using the decorator mode? That is, the class to be decorated is not damaged, that is, the class to be decorated is protected. Instance code: beverage. java [java] public abstract class Beverage {String description = "Unknown Beverage"; String size = "medium"; public String getDescription () {return description ;} public abstract double cost (); public String getSize () {return size;} public void setSize (String size _) {this. size = size _ ;}} CondimentDecorator. java [java] public abstract class CondimentDecorator extends Beverage {public abst Ract String getDescription ();} Espresso. java [java] public class Espresso extends Beverage {public Espresso () {description = "Espresso" ;}public double cost () {return 1.99 ;}} HouseBlend. java [java] public class HouseBlend extends Beverage {public HouseBlend () {description = "HouseBlend" ;}public double cost () {return 0.89 ;}} DarkRoast. java [java] public class DarkRoast extends Beverage {public Da RkRoast () {description = "DarkRoast";} public double cost () {return 0.99 ;}} Mocha. java [java] public class Mocha extends CondimentDecorator {Beverage beverage; public Mocha (Beverage beverage) {this. beverage = beverage;} public String getDescription () {return beverage. getDescription () + ", Mocha";} public double cost () {return 0.2 + beverage. cost () ;}} Soy. java [java] public class Soy extends CondimentDec Orator {Beverage beverage; public Soy (Beverage beverage) {this. beverage = beverage;} public String getDescription () {return beverage. getDescription () + ", Soy";} public double cost () {return 0.15 + beverage. cost () ;}} Whip. java [java] public class Whip extends CondimentDecorator {Beverage beverage; public Whip (Beverage beverage) {this. beverage = beverage;} public String getDescription () {return beverage. GetDescription () + ", Whip";} public double cost () {return 0.10 + beverage. cost () ;}} BeverageSize. java [java] public class BeverageSize extends CondimentDecorator {public double size = 0; public Beverage beverage; public BeverageSize (Beverage beverage) {this. beverage = beverage;} public String getDescription () {return beverage. getDescription () + "," + beverage. getSize ();} public double cost () {switch (bevera Ge. getSize () {case "small": return 0.10 + beverage. cost (); case "medium": return 0.15 + beverage. cost (); case "big": return 0.20 + beverage. cost (); default: return 0.15 + beverage. cost () ;}} StarBuzzCoffee. java [java] public class StarBuzzCoffee {public static void main (String args []) {Beverage beverage = new Espresso (); System. out. println (beverage. getDescription () + '$' + beverage. cost (); Beverage beverag E2 = new DarkRoast (); beverage2 = new Mocha (beverage2); beverage2 = new Mocha (beverage2); beverage2 = new Whip (beverage2); beverage2.setSize ("big "); beverage2 = new BeverageSize (beverage2); System. out. println (beverage2.getDescription () + '$' + response (); Beverage beverage3 = new HouseBlend (); beverage3 = new Mocha (beverage3); beverage3 = new Mocha (beverage3 ); beverage3 = new Whip (beverage3); B Everage3 = new BeverageSize (beverage3); System. out. println (beverage3.getDescription () + '$' + beverage3.cost ();} corresponds to StarBuzzCoffee. the decoration class in the java file uses many new methods. The optimization method is the factory method, which is the place where the specific instance is produced. The factory method is different from the abstract factory method. Abstract concepts, such as abstract classes and interfaces, and methods in abstract classes can have specific implementations, but abstract classes can have their own specific implementation methods; interfaces can only implement method details in their inherited classes (must be implemented ). The common difference between the two is that the inherited class has a public method. Since there is a common method, it is extracted and made into an interface (abstract class ). The following example illustrates how to program interfaces rather than programming: animals. java [java] interface animals {public void bark ();} cat. java [java] class cat implements animals {public void bark () {System. out. println ("miao");} dog. java [java] class dog implements animals {public void bark () {System. out. println ("wang") ;}} test. java [java] public class test {public static void main (String args []) {animals a = new dog (); A. bark () ;}}dog and cat both have a bark method, which is extracted as an interface declaration. For example, for a computer printer, the computer provides a USB interface, No matter any printer as long as the interface is implemented, you can print it. How to implement it is the printer manufacturer's business. If you need the cat bark method now, you only need to change animals a = new dog (); to animals a = new cat. For implementation programming, dog a = new dog (); cat a = new cat ();