从零开始学习设计模式——装饰者模式
2016-07-24 14:18
288 查看
装饰者模式,从字面的理解来说,就是对类的装饰,但是更深层次得来说,其实也是对类的一种扩展。从我们的角度来说,有一种最为熟悉的类扩展方式,就是继承,继承从本质上来说是阐明的“is......a”的关系,但是就装饰者来说,不同的装饰类则是平行的关系,我们先来看UML图:
左侧的ConcreteComponent是我们需要被修饰的实体类,Decorator是装饰类的父类,需要传入需要被实例化类的实例,ConcreteDecoratorA和ConcreteDecoratorB则是具体需要实现的装饰类实体。
代码方面,就用我觉得最贴切的“大圣的72变”来作为例子。
Component接口:
//大圣本尊名号
public interface TheGreatestSage {
public void move();
}
大圣的本体:
//具体的实现类
public class Monkey implements TheGreatestSage{
@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("大圣在移动");
}
}
装饰类:
public class Change implements TheGreatestSage{
public TheGreatestSage sage;
public Change(TheGreatestSage sage){
this.sage = sage;
}
@Override
public void move() {
// 代码
sage.move();
}
}
装饰的实体:
//变成鱼
public class Fish extends Change{
public Fish(TheGreatestSage sage) {
super(sage);
}
@Override
public void move() {
// 代码
sage.move();
System.out.println("Fish Move");
}
}
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
TheGreatestSage sage = new Monkey();
// 第一种写法
TheGreatestSage fish = new Fish(sage);
TheGreatestSage bird = new Bird(fish);
// // 第二种写法
// //TheGreatestSage fish = new Fish(new Bird(sage));
bird.move();
}
}
其实所谓装饰者模式,最最重要的其实也就是其本质——装饰,也就是对于装饰类的编写。打个比方,今天老板看到别的APP有个炫酷的功能说,和你说,这个效果不错嘛,来帮我实现一下。等你哼哧哼哧好不容易做完了,产品经理又找到你说,客户觉得这样的功能不实用,要用回原来的的,那么你需要做的,只是把以上代码中的那一句
给去掉就行了。
面向方法进行修饰,而不是对类进行修改和扩展,这就是装饰者模式的优点。
左侧的ConcreteComponent是我们需要被修饰的实体类,Decorator是装饰类的父类,需要传入需要被实例化类的实例,ConcreteDecoratorA和ConcreteDecoratorB则是具体需要实现的装饰类实体。
代码方面,就用我觉得最贴切的“大圣的72变”来作为例子。
Component接口:
//大圣本尊名号
public interface TheGreatestSage {
public void move();
}
大圣的本体:
//具体的实现类
public class Monkey implements TheGreatestSage{
@Override
public void move() {
// TODO Auto-generated method stub
System.out.println("大圣在移动");
}
}
装饰类:
public class Change implements TheGreatestSage{
public TheGreatestSage sage;
public Change(TheGreatestSage sage){
this.sage = sage;
}
@Override
public void move() {
// 代码
sage.move();
}
}
装饰的实体:
//变成鱼
public class Fish extends Change{
public Fish(TheGreatestSage sage) {
super(sage);
}
@Override
public void move() {
// 代码
sage.move();
System.out.println("Fish Move");
}
}
//变成鸟 public class Bird extends Change{ public Bird(TheGreatestSage sage) { super(sage); } @Override public void move() { // 代码 sage.move(); System.out.println("Bird Move"); } }客户端实现:
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
TheGreatestSage sage = new Monkey();
// 第一种写法
TheGreatestSage fish = new Fish(sage);
TheGreatestSage bird = new Bird(fish);
// // 第二种写法
// //TheGreatestSage fish = new Fish(new Bird(sage));
bird.move();
}
}
其实所谓装饰者模式,最最重要的其实也就是其本质——装饰,也就是对于装饰类的编写。打个比方,今天老板看到别的APP有个炫酷的功能说,和你说,这个效果不错嘛,来帮我实现一下。等你哼哧哼哧好不容易做完了,产品经理又找到你说,客户觉得这样的功能不实用,要用回原来的的,那么你需要做的,只是把以上代码中的那一句
TheGreatestSage bird = new Bird(fish);
给去掉就行了。
面向方法进行修饰,而不是对类进行修改和扩展,这就是装饰者模式的优点。
相关文章推荐
- PropertyChangeListener简单理解
- 什么是设计模式
- 设计模式之创建型模式 - 特别的变量问题
- 七、设计模式——装饰模式
- 设计模式总结
- 设计模式之创建型模式
- 浅谈设计模式的学习
- Ruby设计模式编程之适配器模式实战攻略
- 实例讲解Ruby使用设计模式中的装饰器模式的方法
- 设计模式中的模板方法模式在Ruby中的应用实例两则
- Ruby设计模式编程中对外观模式的应用实例分析
- 实例解析Ruby设计模式编程中Strategy策略模式的使用
- Ruby中使用设计模式中的简单工厂模式和工厂方法模式
- Ruby使用设计模式中的代理模式与装饰模式的代码实例
- 详解组合模式的结构及其在Ruby设计模式编程中的运用
- C# 设计模式系列教程-建造者模式
- C#编程中使用设计模式中的原型模式的实例讲解
- 使用设计模式中的工厂方法模式进行C#编程的示例讲解
- 实例解析C#设计模式编程中简单工厂模式的使用
- 详解C#设计模式编程中生成器模式的使用