装饰模式定义:动态地给一个对象添加额外的职责和功能。也就是说:我们可以在不使用继承、不改变原有结构的基础上扩展新的内容。举个例子,我们都担心摔碎手机屏幕,所以我们可以在手机的外表面裹一层材料(比如钢化膜,手机套)从而使得手机具有了抗摔的新功能;并且手机原本的功能(打电话,拍照,上网)并没有受到任何影响。嗯哼,看完这个小例子,我们来瞅瞅装饰模式中的四个角色:
抚琴路夜市有两样美食:炒饭和炒面。而且,可以在它们中加鸡蛋或者培根。接下来,我就借用这些人间烟火来学习装饰设计模式。
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Food {
private String name;
private double price;
public Food() {}
public Food(String name,double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class FriedRice extends Food {
public FriedRice() {
super("炒饭",12);
}
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class FriedNoodle extends Food {
public FriedNoodle() {
super("炒面",13);
}
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public abstract class Garnish {
private String name;
private double price;
private Food food;
public Garnish() {}
public Garnish(String name, double price, Food food) {
super();
this.name = name;
this.price = price;
this.food = food;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Food getFood() {
return food;
}
public void setFood(Food food) {
this.food = food;
}
// 描述信息
public abstract String desc();
//计算价格
public abstract double cost();
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Egg extends Garnish {
// 宣传语
private String introduce = "美味可口";
// 折扣价
private int discount = 1;
public Egg(Food food) {
super("鸡蛋",2,food);
}
@Override
public String desc() {
String desc = super.getName() + super.getFood().getName() + introduce;
return desc;
}
@Override
public double cost() {
double total = super.getPrice() + super.getFood().getPrice()-discount;
return total;
}
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Bacon extends Garnish {
public Bacon(Food food) {
super("培根",3,food);
}
@Override
public String desc() {
return super.getName() + super.getFood().getName();
}
@Override
public double cost() {
return super.getPrice() + super.getFood().getPrice();
}
}
要点概述:
package com.decoratorPattern03;
/**
* 本文作者:谷哥的小弟
* 博客地址:http://blog.csdn.net/lfdfhl
*/
public class Test {
public static void main(String[] args) {
// 炒饭
FriedRice friedRice = new FriedRice();
System.out.println(friedRice.getName() + " " + friedRice.getPrice() + "元");
System.out.println("---------------------");
// 鸡蛋炒饭
Egg egg = new Egg(new FriedRice());
System.out.println(egg.desc() + " " + egg.cost() + "元");
System.out.println("---------------------");
// 培根炒面
Bacon bacon = new Bacon(new FriedNoodle());
System.out.println(bacon.desc()+ " " + bacon.cost() + "元");
System.out.println("---------------------");
}
}