1. the modifier mode dynamically attaches the responsibility to the object. To expand the functionality, the decorator provides an alternative solution that is more flexible than inheritance.
2. Differences between combination and inheritance
Inheritance. Inheritance is an effective way to add actions to a class. By using inheritance, sub-classes can have their own methods and parent classes. However, inheritance is static, and The subclass behavior is determined during compilation. It is not easy for us to control the method and timing of adding behavior.
Combination. A combination embeds an object into another object, and the other object determines whether to reference the object to expand its own behavior. This is a dynamic method that we can dynamically control in the application.
Compared with inheritance, the advantage of the composite relationship is that it does not damage the encapsulation of classes and has good loose coupling, which makes the system easier to maintain. However, its disadvantage lies in creating more objects than inheriting them.
3. Advantages and disadvantages of the modifier Mode
Advantages
1. the modifier mode provides more flexibility than inheritance.
2. You can use a dynamic method to expand the functions of an object, and select different decorators during runtime to implement different behaviors.
3. You can create a combination of different actions by using different specific decorative classes and the arrangement and combination of these decorative classes. You can use multiple decoration classes to decorate the same object and obtain more powerful objects.
4. The specific component classes and specific decoration classes can be changed independently. You can add new and specific component classes as needed and combine them when using them. The original code does not need to be changed, compliant with the "open and closed principle ".
Disadvantages
1. Many small objects will be generated, increasing the complexity of the system.
2. This is more flexible than inheritance. It also means that the decoration mode is more error-prone than inheritance, and troubleshooting is also very difficult. For objects decorated multiple times, it may be cumbersome to troubleshoot errors during debugging.
4. decorator Use Cases
1. Add roles to a single object dynamically and transparently without affecting other objects.
2. You need to dynamically add functions to an object. These functions can also be dynamically revoked. When the system cannot be expanded by inheritance or the inheritance is not conducive to system expansion and maintenance.
The above content comes from the network
5. UML diagram (astah/Jude): http://pan.baidu.com/s/1eQiVePc
6. Example:
Modifier base class
1 package COM. xinye. test. decoration; 2/** 3 * food base class 4 * @ author Xinye 5*6 */7 public abstract class food {8 9 protected string DESC; 10 11 public abstract string getdesc (); 12}
Chicken
1 package COM. xinye. test. decoration; 2/** 3 * chicken 4 * @ author Xinye 5*6 */7 public class chicken extends food {8 Public chicken () {9 DESC = "chicken "; 10} 11 @ override12 Public String getdesc () {13 return DESC; 14} 15 16}
Duck Meat
1 package COM. xinye. test. decoration; 2/** 3 * duck 4 * @ author Xinye 5*6 */7 public class duck extends food {8 Public duck () {9 DESC = "Duck "; 10} 11 @ override12 Public String getdesc () {13 return DESC; 14} 15 16}
Modifier base class
1 package com.xinye.test.decoration; 2 /** 3 * 4 * @author xinye 5 * 6 */ 7 public abstract class FoodDecoration extends Food { 8 9 @Override10 public abstract String getDesc();11 12 }
Steam-decorator
1 package COM. xinye. test. decoration; 2/** 3 * steamed food 4 * @ author Xinye 5*6 */7 public class steamedfood extends fooddecoration {8 9 private food; 10 11 Public steamedfood (Food f) {12 this. food = f; 13} 14 15 @ override16 Public String getdesc () {17 return getdecoration () + food. getdesc (); 18} 19 20 private string getdecoration () {21 return "steamed"; 22} 23}
Roast-decorator
1 package COM. xinye. test. decoration; 2/** 3 * Roasted Food 4 * @ author Xinye 5*6 */7 public class roastfood extends fooddecoration {8 9 private food; 10 11 Public roastfood (Food f) {12 this. food = f; 13} 14 15 @ override16 Public String getdesc () {17 return getdecoration () + food. getdesc (); 18} 19 20 private string getdecoration () {21 return "Baked"; 22} 23}
Client
1 package COM. xinye. test. decoration; 2/** 3 * client 4 * @ author Xinye 5*6 */7 public class client {8 public static void main (string [] ARGs) {9 // test Pure Food 10 Food F1 = new chicken (); 11 system. out. println (f1.getdesc (); 12 13 system. out. println ("----------------------"); 14 // test single-modified food 15 roastfood Rf = new roastfood (F1); 16 system. out. println (RF. getdesc (); 17 18 system. out. println ("--------------------"); 19 // test food with multiple modifiers 20 steamedfood Sf = new steamedfood (RF); 21 system. out. println (SF. getdesc (); 22} 23}
Execution result:
Chicken
----------------------
Roast chicken
----------------------
Steamed chicken