网站如何做二级栏目,wordpress 云虚拟主机,产品设计网站官网,上海响应式网站建设公司目录 一、结构型模式
1.1、装饰者模式
1.1.1、概念
1.1.2、案例实现
1.1.3、优缺点
1.1.4、使用场景 一、结构型模式 1.1、装饰者模式
1.1.1、概念
装饰者模式就是指在不改变现有对象结构的情况下#xff0c;动态的给该对象增加一些职责#xff08;增加额外功能#…目录 一、结构型模式
1.1、装饰者模式
1.1.1、概念
1.1.2、案例实现
1.1.3、优缺点
1.1.4、使用场景 一、结构型模式 1.1、装饰者模式
1.1.1、概念
装饰者模式就是指在不改变现有对象结构的情况下动态的给该对象增加一些职责增加额外功能的模式.
例如现在要开发一个点餐系统有一个抽象类快餐类快餐店里有炒面、炒饭分别去继承实现. 如果现在需要添加额外的配料比如 鸡蛋那么计算额外加钱就会不叫麻烦就需要分别给 炒面类 和 炒饭类 定义一个子类鸡蛋炒面类、鸡蛋炒饭类. 再者如果要新增一个快餐类比如 凉皮就需要定义更多的子类. 通过装饰者模式就可以在不需要添加子类的情况下动态的添加配料.
装饰者模式中的角色如下
抽象构件角色定义一个抽象接口用来规范附加的对象比如上述的 快餐类.具体构件角色实现抽象构件将来会被添加一些职责比如上述的 炒面类 和 炒饭类.抽象装饰角色继承抽象构件 和 持有抽象构件的引用可以通过 子类 扩展具体的构件.具体装饰角色继承抽象装饰重写相关方法给具体的构件对象添加附加责任. 1.1.2、案例实现
实现上述案例.
/*** 抽象构建角色: 快餐*/
public abstract class FastFood {private float price; //价格private String desc; //描述public abstract float cost(); //获取最终价格public FastFood() {}public FastFood(float price, String desc) {this.price price;this.desc desc;}public float getPrice() {return price;}public void setPrice(float price) {this.price price;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc desc;}}/*** 具体构建角色: 炒饭类*/
public class Rice extends FastFood{public Rice() {super(10, 炒饭);}Overridepublic float cost() {return getPrice();}}/*** 具体构建角色 面条类*/
public class Noodles extends FastFood {public Noodles() {super(12, 炒面);}Overridepublic float cost() {return getPrice();}}/*** 抽象装饰角色: 配料类*/
public abstract class Garnish extends FastFood {private FastFood fastFood;public FastFood getFastFood() {return fastFood;}public void setFastFood(FastFood fastFood) {this.fastFood fastFood;}public Garnish(FastFood fastFood, float price, String desc) {super(price, desc);this.fastFood fastFood;}}/*** 抽象装饰角色: 配料类*/
public abstract class Garnish extends FastFood {private FastFood fastFood;public FastFood getFastFood() {return fastFood;}public void setFastFood(FastFood fastFood) {this.fastFood fastFood;}public Garnish(FastFood fastFood, float price, String desc) {super(price, desc);this.fastFood fastFood;}}/*** 具体装饰角色: 鸡蛋配料*/
public class Egg extends Garnish{public Egg(FastFood fastFood) {super(fastFood, 1, 鸡蛋);}Overridepublic float cost() {return getPrice() getFastFood().cost();}Overridepublic String getDesc() {return super.getDesc() getFastFood().getDesc();}}/*** 具体装饰角色: 培根配料*/
public class Bacon extends Garnish{public Bacon(FastFood fastFood) {super(fastFood, 2, 培根);}Overridepublic float cost() {return getPrice() getFastFood().cost();}Overridepublic String getDesc() {return super.getDesc() getFastFood().getDesc();}}public class Client {public static void main(String[] args) {//1.带你一份炒饭FastFood food new Rice();System.out.println(food.getDesc() food.cost() 元);System.out.println();//2.加一份鸡蛋food new Egg(food);System.out.println(food.getDesc() food.cost() 元);System.out.println();//3.加一份鸡蛋food new Egg(food);System.out.println(food.getDesc() food.cost() 元);System.out.println();//4.加一个培根food new Bacon(food);System.out.println(food.getDesc() food.cost() 元);}}执行结果如下 1.1.3、优缺点 优点 动态扩展比继承具有更好的扩展性继承时静态附加责任装饰者时动态添加责任可以在不修改原类的情况下给对象添加责任. 缺点 增加系统复杂度装饰者比继承使用的类可能会少但比继承使用的对象可能更多更多的对象在程序排查错误的时候可能更复杂. 1.1.4、使用场景
1. 当不能采用继承的方式对程序进行扩展比如存在大量的独立扩展为了支持每一种组合产生大量子类例如上述栗子中的给 炒饭加鸡蛋导致出现 鸡蛋炒饭类导致子类数目爆炸增长、或者是 类定位为 final 类型不能继承.
2. 在不影响其他类的情况下以动态的方式添加责任.