当前位置:网站首页>[designmode] Decorator Pattern

[designmode] Decorator Pattern

2022-07-06 00:08:00 weixin_ forty-three million two hundred and twenty-four thousan

Basic introduction

  1. Decorator mode : Dynamically attach new functions to objects . In terms of object function expansion , It's more flexible than inheritance , The decorator pattern also embodies the principle of opening and closing (ocp)

  2. The dynamic addition of new functions to objects and ocp principle , In the later application examples, it will be reflected in the form of code , Please pay attention to the experience .

1. Starbucks Coffee order project ( café ):

  1. Types of coffee / Single coffee :Espresso( Espresso )、ShortBlack、LongBlack( Cafe Americano )、Decaf( No cause coffee )

  2. seasoning :Milk、Soy( Soya-bean milk )、Chocolate

  3. When it comes to expanding new types of coffee , It has good expansibility 、 It's easy to change 、 Convenient maintenance

  4. Use OO To calculate the cost of different kinds of coffee : Customers can order a single coffee , You can also have coffee alone + Seasoning mix .

2. programme 1- Solve Starbucks Coffee order project

Poor scheme 1

Class diagram :
 Insert picture description here

3. programme 1- Solve Starbucks Coffee order problem analysis

  1. Drink Is an abstract class , It means drink

  2. des It's a description of coffee , Like the name of the coffee

  3. cost() The way is to calculate the cost ,Drink Class to make an abstract method . 4) Decaf It's a single coffee , Inherit Drink, And implement cost

  4. Espres

原网站

版权声明
本文为[weixin_ forty-three million two hundred and twenty-four thousan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207052357214627.html