Java design pattern (3)-modifier Pattern
We all know that you can add behavior to a class or object in two ways.
First, use 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.
Second, use Association. 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 association 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.
1. decorator definition:
The decoration mode dynamically attaches the responsibility to the object. If you want to expand the function, the decoration provides an alternative solution that is more flexible than inheritance.
Ii. UML diagrams decorated with patterns:
Compoent class: abstract class. Each component can be used independently or packaged for use.
ConcreteCompoent class: the object that dynamically loads a new row, inherited from Compoent
Decorator class: Describe the commonly implemented interfaces, which can make abstract classes
ConcreteDecorator1 and ConcreteDecorator2: decoration instance with Compent
Iii. application scenarios: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Environment/environment/so6zL + Environment/Environment + Environment/p7fIyrGjrL/environment/Environment + 8wse1vbX3wc + Environment + CtXiysfL + Environment "http://www.2cto.com/uploadfile/Collfiles/20140728/20140728091454233.png" alt = "\">
Solution:
4. write code
Compotemt: Beverage class
//@file:beverage.javapublic abstract class Beverage {String description = "Unknown Beverage";public String getDescription(){return description;}public abstract double cost();}
ConcreteCompotemt: Espresso class
// Espresso // @ file: Espresso. javapublic class Espresso extends Beverage {public Espresso () {description = "Espresso" ;}@ Overridepublic double cost () {return 1 ;}}CondimentDecorator: HouseBlend class
// @ File: HouseBlend. java, // deep baking public class HouseBlend extends Beverage {public HouseBlend () {description = "HouseBlend" ;}@ Overridepublic double cost () {return 0.5 ;}}Decorator: CondimentDecorator class
//@file:CondimentDecorator.javapublic abstract class CondimentDecorator extends Beverage{abstract public String getDescription();}
ConcreteDecorator: Milk class
// File: Milk. java Milk flavor seasoning public class Milk extends CondimentDecorator {Beverage beverage; public Milk (Beverage beverage) {this. beverage = beverage;} @ Overridepublic String getDescription () {return beverage. getDescription () + "-Milk" ;}@ Overridepublic double cost () {return beverage. cost () + 2.0 ;}}
ConcreteDecorator: Mocha class
// Moka flavor seasoning public class Mocha extends CondimentDecorator {Beverage beverage; public Mocha (Beverage beverage) {this. beverage = beverage;} @ Overridepublic String getDescription () {return beverage. getDescription () + "-Mocha" ;}@ Overridepublic double cost () {return beverage. cost () + 3.0 ;}}
ConcreteCDecorator: Soy class
// Soy milk flavor seasoning public class Soy extends CondimentDecorator {Beverage beverage; public Soy (Beverage beverage) {this. beverage = beverage;} @ Overridepublic String getDescription () {return beverage. getDescription () + "-Soy" ;}@ Overridepublic double cost () {return beverage. cost () + 4.0 ;}}
Order now !!!
public class OrderApp {public static void main(String[] args) {Beverage beverage1 = new HouseBlend();beverage1 = new Milk(beverage1);beverage1 = new Mocha(beverage1);beverage1 = new Soy(beverage1);System.out.println("beverage1 description:"+beverage1.getDescription()+"\ncost:"+beverage1.cost());Beverage beverage2 = new Espresso();beverage2= new Milk(beverage2);beverage2 = new Mocha(beverage2);System.out.println("beverage2 description:"+beverage2.getDescription()+"\ncost:"+beverage2.cost());}}
Output:
Beverage1 description: HouseBlend-Milk-Mocha-Soy
Cost: 9.5
Beverage2 description: Espresso-Milk-Mocha
Cost: 6.0