装饰者模式(Decorator)

装饰者模式可以动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

该模式的适用环境为:

(1)在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

(2)处理那些可以撤消的职责。

(3)当不能采用生成子类的方法进行扩充时。一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

实现该模式的关键步骤:

(1)Component(被装饰对象基类):定义对象的接口,可以给这些对象动态增加职责;

(2)ConcreteComponent(具体被装饰对象):定义具体的对象,Decorator可以给它增加额外的职责;

(3)Decorator(装饰者抽象类):维护指向Component实例的引用,定义与Component一致的接口(也就是要继承或实现被装饰对象基类);

(4)ConcreteDecorator(具体装饰者):具体的装饰对象,给内部持有的具体被装饰对象增加具体的职责;

/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 抽象构件(被装饰者的抽象), 可以是抽象类
*/
public interface Human {
public void wearClothes();

public void walkToWhere();
}
/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 定义具体被装饰者,被装饰者初始状态有些自己的装饰
*/
public class Person implements Human {
@Override
public void wearClothes() {
System.out.println("穿什么呢。。");
}

@Override
public void walkToWhere() {
System.out.println("去哪里呢。。");
}
}
/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 抽象装饰者类
*/
public abstract class Decorator implements Human {
private Human human;

public Decorator(Human human) {
this.human = human;
}

public void wearClothes() {
human.wearClothes();
}

public void walkToWhere() {
human.walkToWhere();
}
}
/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 具体装饰者
*/
public class Decorator_zero extends Decorator{
public Decorator_zero(Human human) {
super(human);
}

public void goHome() {
System.out.println("进房子。。");
}

public void findMap() {
System.out.println("书房找找Map。。");
}

@Override
public void wearClothes() {

super.wearClothes();
goHome();
}

@Override
public void walkToWhere() {
super.walkToWhere();
findMap();
}
}
/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 具体装饰者
*/
public class Decorator_first extends Decorator {
public Decorator_first(Human human) {
super(human);
}

public void goClothespress() {
System.out.println("去衣柜找找看。。");
}

public void findPlaceOnMap() {
System.out.println("在Map上找找。。");
}

@Override
public void wearClothes() {
super.wearClothes();
goClothespress();
}

@Override
public void walkToWhere() {
super.walkToWhere();
findPlaceOnMap();
}
}
/**
* Created by kentorvalds on 2018/3/6.
* 装饰者模式: 具体装饰者
*/
public class Decorator_two extends Decorator {

public Decorator_two(Human human) {
super(human);
}

public void findClothes() {
System.out.println("找到一件D&G。。");
}

public void findTheTarget() {
System.out.println("在Map上找到神秘花园和城堡。。");
}

@Override
public void wearClothes() {
super.wearClothes();
findClothes();
}

@Override
public void walkToWhere() {
super.walkToWhere();
findTheTarget();
}
}
//测试类,跟java的I/O操作有多么相似
public class Test {
public static void main(String[] args) {
Human person = new Person();
Decorator decorator = new Decorator_two(new Decorator_first(new Decorator_zero(person)));//?
decorator.wearClothes();
decorator.walkToWhere();
}
}
关键点:
1、Decorator抽象类中,持有Human接口,方法全部委托给该接口调用,目的是交给该接口的实现类即子类进行调用。
2、Decorator抽象类的子类(具体装饰者),里面都有一个构造方法调用super(human),这一句就体现了抽象类依赖于子类实现即抽象依赖于实现的原则。因为构造里面参数都是Human接口,只要是该Human的实现类都可以传递进去,即表现出Decorator dt = new Decorator_second(new Decorator_first(new Decorator_zero(human)));这种结构的样子。所以当调用dt.wearClothes();dt.walkToWhere()的时候,又因为每个具体装饰者类中,都先调用super.wearClothes和super.walkToWhere()方法,而该super已经由构造传递并指向了具体的某一个装饰者类(这个可以根据需要调换顺序),那么调用的即为装饰类的方法,然后才调用自身的装饰方法,即表现出一种装饰、链式的类似于过滤的行为。
3、具体被装饰者类,可以定义初始的状态或者初始的自己的装饰,后面的装饰行为都在此基础上一步一步进行点缀、装饰。
4、装饰者模式的设计原则为:对扩展开放、对修改关闭,这句话体现在我如果想扩展被装饰者类的行为,无须修改装饰者抽象类,只需继承装饰者抽象类,实现额外的一些装饰或者叫行为即可对被装饰者进行包装。所以:扩展体现在继承、修改体现在子类中,而不是具体的抽象类,这充分体现了依赖倒置原则.


原文地址:https://www.cnblogs.com/kexianting/p/8512993.html