02.装饰者模式
1、认识装饰者模式
购买咖啡时,除了咖啡的各种口味以外,还可以加入各种调料,例如:蒸奶、豆浆、摩卡或奶盖,星巴克会根据加入不同的调料收取不同的费用,当然还有大中小杯等等。
如果我们使用类继承的方式,会导致类数目爆炸、设计死板,以及基类加入的新功能并不适用于所有的子类。
所以我们要采取不一样的做法,我们以饮料为主体,然后采用调料类、体积类来修饰。
(1)拿一个深焙咖啡(DarkRoast)对象
(2)以摩卡(Mocha)对象来修饰
(3)以奶泡(Whip)来修饰
(4)以中杯(Medium)来修饰
(5)调用cost()方法,并依赖委托(delegate)将调料的价钱加上
2、定义
装饰者模式:动态地将责任附加到对象上,若要拓展功能,装饰者提供了比继承更有弹性的替代方案。
BD65E96F-442F-4A17-8CAD-429B18D95F1D_1_105_c.jpeg3、设计原则:
开放-关闭原则
类应该对扩展开放,对修改关闭。
我们的目标是允许类容易拓展,在不修改现有代码的情况下,就可搭配新的行为。
4、设计
5DC568F8-7135-4D6B-B40D-D48673A49C73_1_201_a.jpeg代码实现:
(1)装饰者总抽象类
public abstract class Beverage {
public String description = "Unknown Beverage";
public String getDescription(){
return description;
}
public abstract double cost();
}
(2)调料装饰者 抽象类
public abstract class CondimentDecorator extends Beverage {
@Override
public abstract String getDescription();
}
(3)容量装饰者 抽象类
public abstract class Capacity extends Beverage {
@Override
public abstract String getDescription();
}
(4)四种咖啡实现类
/**
* 深度烘焙咖啡类
*/
public class DarkRoast extends Beverage {
public DarkRoast(){
this.description = "DarkRoast";
}
@Override
public double cost() {
return 2.99;
}
}
/**
* 猫屎咖啡
*/
public class Decat extends Beverage {
public Decat(){
this.description = "Decat";
}
@Override
public double cost() {
return 1.89;
}
}
/**
* 浓缩咖啡类
*/
public class Espresso extends Beverage {
public Espresso(){
this.description = "Espresso";
}
@Override
public double cost() {
return 1.99;
}
}
/**
* 首选咖啡(星巴克咖啡名)
*/
public class HouseBlend extends Beverage {
public HouseBlend() {
description = "HouseBlend";
}
@Override
public double cost() {
return 0.89;
}
}
(5)调料类
/**
* 摩卡 调料类
*/
public class Mocha extends CondimentDecorator {
private Beverage beverage;
public Mocha(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Mocha";
}
@Override
public double cost() {
return .20 + beverage.cost();
}
}
/**
* 大豆 调料类
*/
public class Soy extends CondimentDecorator {
private Beverage beverage;
public Soy(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Soy";
}
@Override
public double cost() {
return .30 + beverage.cost();
}
}
/**
* 鲜奶 调料类
*/
public class Whip extends CondimentDecorator {
private Beverage beverage;
public Whip(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Whip";
}
@Override
public double cost() {
return .50 + beverage.cost();
}
}
(6)容量类
/**
* 大杯
*/
public class Big extends Capacity {
private Beverage beverage;
public Big(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.20;
}
@Override
public String getDescription() {
return beverage.getDescription() + " ,Big";
}
}
/**
* 中杯
*/
public class Medium extends Capacity {
private Beverage beverage;
public Medium(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.15;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + " ,Medium";
}
}
/**
* 小杯
*/
public class Small extends Capacity {
private Beverage beverage;
public Small(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.10;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + " ,Small";
}
}
(7)测试类
public class StarbuzzCoffee {
public static void main(String[] args) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " $" + beverage.cost());
Beverage beverage1 = new DarkRoast();
beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);
System.out.println(beverage1.getDescription() + " $" + beverage1.cost());
Beverage beverage2 = new HouseBlend();
beverage2 = new Soy(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
beverage2 = new Big(beverage2);
System.out.println(beverage2.getDescription() + " $" + beverage2.cost());
}
}
结果:
Espresso $1.99
DarkRoast,Mocha,Mocha,Whip $3.8900000000000006
HouseBlend,Soy,Mocha,Whip ,Big $2.09
1、认识装饰者模式
购买咖啡时,除了咖啡的各种口味以外,还可以加入各种调料,例如:蒸奶、豆浆、摩卡或奶盖,星巴克会根据加入不同的调料收取不同的费用,当然还有大中小杯等等。
如果我们使用类继承的方式,会导致类数目爆炸、设计死板,以及基类加入的新功能并不适用于所有的子类。
所以我们要采取不一样的做法,我们以饮料为主体,然后采用调料类、体积类来修饰。
(1)拿一个深焙咖啡(DarkRoast)对象
(2)以摩卡(Mocha)对象来修饰
(3)以奶泡(Whip)来修饰
(4)以中杯(Medium)来修饰
(5)调用cost()方法,并依赖委托(delegate)将调料的价钱加上
2、定义
装饰者模式:动态地将责任附加到对象上,若要拓展功能,装饰者提供了比继承更有弹性的替代方案。
BD65E96F-442F-4A17-8CAD-429B18D95F1D_1_105_c.jpeg3、设计原则:
开放-关闭原则
类应该对扩展开放,对修改关闭。
我们的目标是允许类容易拓展,在不修改现有代码的情况下,就可搭配新的行为。
4、设计
5DC568F8-7135-4D6B-B40D-D48673A49C73_1_201_a.jpeg代码实现:
(1)装饰者总抽象类
public abstract class Beverage {
public String description = "Unknown Beverage";
public String getDescription(){
return description;
}
public abstract double cost();
}
(2)调料装饰者 抽象类
public abstract class CondimentDecorator extends Beverage {
@Override
public abstract String getDescription();
}
(3)容量装饰者 抽象类
public abstract class Capacity extends Beverage {
@Override
public abstract String getDescription();
}
(4)四种咖啡实现类
/**
* 深度烘焙咖啡类
*/
public class DarkRoast extends Beverage {
public DarkRoast(){
this.description = "DarkRoast";
}
@Override
public double cost() {
return 2.99;
}
}
/**
* 猫屎咖啡
*/
public class Decat extends Beverage {
public Decat(){
this.description = "Decat";
}
@Override
public double cost() {
return 1.89;
}
}
/**
* 浓缩咖啡类
*/
public class Espresso extends Beverage {
public Espresso(){
this.description = "Espresso";
}
@Override
public double cost() {
return 1.99;
}
}
/**
* 首选咖啡(星巴克咖啡名)
*/
public class HouseBlend extends Beverage {
public HouseBlend() {
description = "HouseBlend";
}
@Override
public double cost() {
return 0.89;
}
}
(5)调料类
/**
* 摩卡 调料类
*/
public class Mocha extends CondimentDecorator {
private Beverage beverage;
public Mocha(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Mocha";
}
@Override
public double cost() {
return .20 + beverage.cost();
}
}
/**
* 大豆 调料类
*/
public class Soy extends CondimentDecorator {
private Beverage beverage;
public Soy(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Soy";
}
@Override
public double cost() {
return .30 + beverage.cost();
}
}
/**
* 鲜奶 调料类
*/
public class Whip extends CondimentDecorator {
private Beverage beverage;
public Whip(Beverage beverage){
this.beverage = beverage;
}
@Override
public String getDescription() {
return beverage.getDescription() + ",Whip";
}
@Override
public double cost() {
return .50 + beverage.cost();
}
}
(6)容量类
/**
* 大杯
*/
public class Big extends Capacity {
private Beverage beverage;
public Big(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.20;
}
@Override
public String getDescription() {
return beverage.getDescription() + " ,Big";
}
}
/**
* 中杯
*/
public class Medium extends Capacity {
private Beverage beverage;
public Medium(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.15;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + " ,Medium";
}
}
/**
* 小杯
*/
public class Small extends Capacity {
private Beverage beverage;
public Small(Beverage beverage){
this.beverage = beverage;
}
@Override
public double cost() {
return beverage.cost() + 0.10;
}
@Override
public String getDescription() {
return this.beverage.getDescription() + " ,Small";
}
}
(7)测试类
public class StarbuzzCoffee {
public static void main(String[] args) {
Beverage beverage = new Espresso();
System.out.println(beverage.getDescription() + " $" + beverage.cost());
Beverage beverage1 = new DarkRoast();
beverage1 = new Mocha(beverage1);
beverage1 = new Mocha(beverage1);
beverage1 = new Whip(beverage1);
System.out.println(beverage1.getDescription() + " $" + beverage1.cost());
Beverage beverage2 = new HouseBlend();
beverage2 = new Soy(beverage2);
beverage2 = new Mocha(beverage2);
beverage2 = new Whip(beverage2);
beverage2 = new Big(beverage2);
System.out.println(beverage2.getDescription() + " $" + beverage2.cost());
}
}
结果:
Espresso $1.99
DarkRoast,Mocha,Mocha,Whip $3.8900000000000006
HouseBlend,Soy,Mocha,Whip ,Big $2.09
网友评论