Structure diagram interpretation:
/** * SX I sell snacks here * Chicken leg rice (chicken leg + green vegetables + dried sesame) * Rice with shredded pork (shredded pork + green vegetables + dried sesame) * Large row rice (large row + green vegetables + dried sesame) * Duck leg rice (duck leg + green vegetables + dried sesame) * Dongpo pilaf (Dongpo + green vegetables + dried parsley) * @author wrj * @description * @Date 2021/12/1 2:37 afternoon */
abstract class Food { public abstract void has(); } //rice class Rice extends Food{ @Override public void has() { System.out.println("There is rice"); } } //Decoration class abstract class FoodDecorator extends Food{ public Food food; public void addFood(Food food){ this.food = food; } @Override public void has() { food.has(); } } //drumsticks class Drumstick extends FoodDecorator{ @Override public void has() { System.out.println("There are chicken legs"); super.has(); } } //shredded meat class ShreddedMeat extends FoodDecorator{ @Override public void has() { System.out.println("Shredded meat"); super.has(); } } //Platoon class PorkRibs extends FoodDecorator{ @Override public void has() { System.out.println("There are large rows"); super.has(); } } //Green vegetables class Vegetable extends FoodDecorator{ @Override public void has() { System.out.println("There are vegetables"); super.has(); } } //Dried tofu class DriedBeanCurd extends FoodDecorator{ @Override public void has() { System.out.println("There are dried beans"); super.has(); } } public class SXFood {
//Variables written in Chinese are not standard for ease of understanding public static void main(String[] args) { //Construction process of standard chicken leg rice System.out.println("Rice with Stewed Drumstick:\r\n"); Rice rice = new Rice(); DriedBeanCurd Dried tofu = new DriedBeanCurd(); Vegetable Green vegetables = new Vegetable(); Drumstick drumsticks = new Drumstick(); drumsticks.addFood(Green vegetables); Green vegetables.addFood(Dried tofu); Dried tofu.addFood(rice); drumsticks.has(); //Special needs chicken leg rice+Platoon System.out.println("Rice with Stewed Drumstick+Platoon:\r\n"); Rice Rice 1 = new Rice(); DriedBeanCurd Dried bean 1 = new DriedBeanCurd(); Vegetable Green vegetables 1 = new Vegetable(); Drumstick Drumstick 1 = new Drumstick(); PorkRibs Platoon = new PorkRibs(); Drumstick 1.addFood(Platoon); Platoon.addFood(Green vegetables 1); Green vegetables 1.addFood(Dried bean 1); Dried bean 1.addFood(Rice 1); Drumstick 1.has(); } }
Final output:
Summary:
Decoration mode is a way to dynamically add more functions for existing functions. When the system needs new functions, it is to add new code to the old classes. This new code usually decorates the core responsibilities or main behaviors of the original class.
New fields, new methods and new logic are added to the main class, thus increasing the complexity of the main class. These newly added things are only to meet the needs of some special behaviors that can only be performed under certain circumstances (refer to the scene of chicken leg rice plus a large row).
The decoration pattern provides a very good solution by putting each function to be decorated in a separate class. And let this class wrap the object it wants to decorate. Therefore, when special behaviors need to be performed, the customer code can selectively and sequentially use the decoration function to wrap objects at run time.
Advantages: move the decoration function in the class out of the class and simplify the original class. Effectively distinguish the core responsibilities of classes from decorative functions.