前篇——软件设计模式-基础
前篇——软件色痕迹模式-三种工厂模式
装饰模式是结构型模式
目录
1.问题引入
买煎饼果子,低价8元,加蛋加肠另付。
可以用继承的方法来实现。就是煎饼果子作为父类,加蛋(肠)的煎饼果子作为子类。
//煎饼果子类
public class Battercake{
//描述
public String getDesc(){
return "煎饼果子";
}
//价格
public int return cost(){
return 8;
}
}
//加蛋继承煎饼果子
public class BattercakeWithEgg extends Battercake{
//重写描述
public String getDesc(){
return super.getDesc()+"加个蛋";
}
//重写价格
public int return cost(){
return super.cost()+1;
}
}
//加肠继承煎饼果子
public class BattercakeWithSausage extends Battercake{
//重写描述
public String getDesc(){
return super.getDesc()+"加个肠";
}
//重写价格
public int return cost(){
return super.cost()+2;
}
}
//测试类
public class test{
public static void main(String args[]){
//来个果子
Battercake battercake = new Battercake();
System.out.println(battercake.getDesc()+battercake.cost()元);
//果子加个蛋
Battercake battercakewithegg = new BattercakeWithEgg();
System.out.println(battercakewithegg.getDesc()+battercakewitheggs.cost()元);
//果子加个肠
Battercake battercakewithsausage = new BattercakeWithSausage();
System.out.println(battercakewithsausage.getDesc()+battercakewithsausage.cost()元);
}
}
直接用继承实现的问题:
导致类太多,类成倍增长就会出现类爆炸
2.解决—装饰者模式
装饰者模式是一种对象结构型模式
2.1 定义
定义:指在不改变对象现有对象结构的情况下,动态的给对象增加一些额外的职责(即增加其他功能的模式)的模式。
增加对象功能来说,装饰者模式比生成子类更为灵活
结构型模式之一,通过装饰者持有组件(被装饰者)的引用,也含有调用组件的方法。而这两个往往两个都是抽象,都有具体的实现。那么在具体的装饰者中,调用具体的组件方法,使用super方式来调用,再其调用前后可以自由添加想用的新增方法,这新增方法就是装饰了。
2.2 面向抽象编程
2.2.1 用装饰者模式解决上述问题
//抽象的煎饼果子(抽象组件)
public abstract class AbstractBattercake{
//得到加东西项的信息
public abstract String getDesc();
//得到费用
public abstract int getcost();
}
//煎饼果子(具体组件)
public class Battercake extends AbstractBattercake{
//实现两个抽象方法
public String getDesc(){
return "煎饼果子";
}
public int getCost(){
return 8;
}
}
//抽象装饰
public class DecocatorCake extends AbstractBattercake{
//变量:来存储被装饰者对象
private AbstractBattercake battercake;
//通过构造方法给变量赋值
public DecocatorCake(AbstractBattercake battercake){
this.battercake =battercake;
}
//保留无参的构造方法
public DecocatorCake(){}
public String getDesc(){
return this.battercake.getDesc();
}
public int getCost(){
return this.battercake.getCost();
}
}
//具体装饰1加蛋(继承抽象装饰)
public class AddEgg extends DecorateCake{
public AddEgg(){}
public AddEgg(AbstractBattercake battercake){
super(battercake);
}
public String getDesc(){
return super.getDesc()+"加一个蛋";
}
public int getCost(){
return super.getCost()+1;
}
}
//具体装饰2加肠(继承抽象装饰)
public class AddSausage extends DecoratetorCake{
public AddSausage(){}
public AddSausage(AbstractBattercake battercake){
super(battercake);
}
public String getDesc(){
return super.getDesc()+"加一根肠";
}
public int getCost(){
return super.getCost()+2;
}
}
//测试类
public class Demo{
public static void main(String args[]){
AbstractBattercake battercake = null;
battercake = new Battercake();
battercake = new AddEgg(battercake);
battercake = new AddSausage(battercake);
System.out.println(battercake.getcake()+"价格"+batter.cake.getCost());
}
}
2.3 结构
2.3.1 四种角色(通过上述例子辅助理解)
1,2是被装饰者。3,4是装饰者
- 抽象组件(AbstractBattercake):抽象组件是一个抽象类,抽象组件定义了被装饰者需要进行装饰的方法。
- 具体组件(Battercake):具体组件是抽象组件的一个子类,具体组件的实例被称作"被装饰者"
- 装饰(DecocatorCake):装饰也是抽象组件的一个子类,但装饰还包含一个抽象组件声明的变量,以保存”被装饰者“的引用。装饰可以是抽象类,也可以是一个非抽象类,如果是非抽象类,那么该类的实例称为“装饰者”。
- 具体装饰(AddEgg、AddSausage):具体装饰是装饰的一个非抽象子类,具体装饰的实例称作装饰者。
2.3.2 结构类图
2.4 优缺点
优点:
- 扩展对象功能灵活
- 装饰者与被装饰者是松耦合关系
- 使用灵活(使用不同装饰类以及这些类的排列组合)
- 满足开闭原则
缺点
多层装饰比较复杂
2.5 应用场景:
- 扩展类功能或添加职责,又不能生成子类的方法进行扩充
- 对象的功能要求可以动态变更的时候(动态增加、动态撤销)
- 需要非常多功能,而这些功能可以通过 基本功能的排列组合实现(继承很难实现时)