装饰器模式
来源:互联网 发布:数据库安全方案 编辑:程序博客网 时间:2024/06/12 00:43
装饰器模式:动态地为对象添加一些额外的功能。
特点:
1、装饰者和被装饰者拥有相同的超类型,可以是接口可以是抽象类。
2、装饰类中包含一个被装饰类组件的引用
3、可以为被装饰对象添加额外的功能
4、在装饰类中重写被装饰组件类中的方法,并对被装饰组件类中对应的方法调用。
结构图:
1、Component是装饰器模式中公共方法的类,在装饰器模式的顶层。
2、ConcreateComponent是转换器模式中具体被装饰的类,IO包中的媒体流就是此种对象。
3、Decorator 装饰器模式的核心对象,所有具体装饰类对象的父类,完成装饰器的部分职能,继承自Component,同时包含一个Component作为成员变量,装饰器模式中动态添加的功能是在这里实现的。
4、ConcreteDecoratorA和ConcreteDecoratorB是两个具体的装饰器对象,他们完成具体的装饰功能,装饰功能的实现通过调用被装饰对象对应的方法,加上装饰对象自身的方法,这是装饰模式中动态添加额外功能的关键。
/**
* 所有类的超类
* @author mcx
*
*/
public abstract class Ingredient {
public abstract String getDescription();
public abstract double getPrice();
public void print(){
System.out.println("name "+this.getDescription());
System.out.println("price "+this.getPrice());
}
}
public class Bread extends Ingredient{
private String description;
public Bread(String description){
this.description = description;
}
@Override
public String getDescription() {
return description;
}
@Override
public double getPrice() {
return 3;
}
}
public abstract class Decorator extends Ingredient{
protected Ingredient ingredient;
public Decorator(Ingredient ingredient){
this.ingredient = ingredient;
}
public abstract String getDescription();
public abstract double getPrice();
}
public class Beef extends Decorator{
public Beef(Ingredient ingredient) {
super(ingredient);
}
@Override
public String getDescription() {
return ingredient.getDescription() +" decorated with beef";
}
@Override
public double getPrice() {
double beef = 4;
double bread = ingredient.getPrice();
return bread + beef;
}
}
public class GreenGrocery extends Decorator{
public GreenGrocery(Ingredient ingredient) {
super(ingredient);
}
@Override
public String getDescription() {
return ingredient.getDescription() +" decorated with GreenGrocery";
}
@Override
public double getPrice() {
return ingredient.getPrice() + 2;
}
}
public class Mutton extends Decorator{
public Mutton(Ingredient ingredient) {
super(ingredient);
}
@Override
public String getDescription() {
return ingredient.getDescription() + " decorated with mutton";
}
@Override
public double getPrice() {
return ingredient.getPrice() + 4;
}
}
public class Pork extends Decorator{
public Pork(Ingredient ingredient){
super(ingredient);
}
@Override
public String getDescription() {
return ingredient.getDescription() + " decorated with pork";
}
@Override
public double getPrice() {
return ingredient.getPrice() + 4;
}
}
public class Salard extends Decorator{
public Salard(Ingredient ingredient) {
super(ingredient);
}
@Override
public String getDescription() {
return ingredient.getDescription()+" decorated with salard";
}
@Override
public double getPrice() {
return ingredient.getPrice() + 3;
}
}
public class Test {
public static void main(String[] args) {
Ingredient sand1 = new Beef(new GreenGrocery(new Bread(" bread with milk")));
Ingredient sand2 = new Mutton(new Beef(new Pork(new Bread(" bread with cheese"))));
Ingredient sand3 = new Beef(new Salard(new GreenGrocery(new Bread("bread with milk"))));
sand1.print();
sand2.print();
sand3.print();
}
}
- c++装饰器模式
- 装饰器模式
- 装饰器模式
- 装饰器(Decorator)模式
- 装饰器模式
- 装饰器模式
- 装饰器模式
- 装饰器模式
- 装饰器模式
- 装饰器模式
- 装饰器(Decorator)模式
- 浅谈装饰器模式
- java 装饰器模式
- 装饰器(Decorator)模式
- 装饰器模式
- 装饰器(Decorator)模式
- 装饰器模式
- 装饰器模式
- 小甲鱼PE详解之IMAGE_DOS_HEADER结构定义即各个属性的作用(PE详解01)
- 编写sublime插件
- VMware vSphere 虚拟化简介
- http服务器
- Oracle中的DDL,DML,DCL总结
- 装饰器模式
- Android Studio导入项目非常慢的解决办法
- ofbiz的webservice接口提供(4)-支持复杂数据类型方法
- 立体感金属管
- 引用外部.css或.js文件的路径问题--jsp或html页面或是在web项目中的引用
- lower_bound()返回值
- Javascript之删除数组a在数组b中的元素
- 河南林业农业物联网水肥一体化
- Eclipse常用快捷键大全