问:装饰者模式有什么用?
答:装饰模式可以在不创造更多子类的情况下,将对象的功能加以扩展。
下面给出一个小例子
1.类图如下:
Person接口:
package decorator.DIYDecorator;
public interface Person {
void function();
}
CaiXuKun类:
package decorator.DIYDecorator;
public class CaiXuKun implements Person {
public void function() {
System.out.println("CaiXuKun能唱歌");
}
}
Singer类:
package decorator.DIYDecorator;
public class Singer implements Person {
public void function() {
System.out.println("Singer能唱歌");
}
}
Decorator类:
package decorator.DIYDecorator;
public class Decorator implements Person {
private Person person;
public Decorator(Person person) {
this.person = person;
}
@Override
public void function() {
person.function();
}
}
Decorator01类:
package decorator.DIYDecorator;
public class Decorator01 extends Decorator{
public Decorator01(Person person) {
super(person);
// TODO Auto-generated constructor stub
}
@Override
public void function() {
super.function();
System.out.println("添加额外功能01:跳舞");
}
}
Decorator02类:
package decorator.DIYDecorator;
public class Decorator02 extends Decorator{
public Decorator02(Person person) {
super(person);
// TODO Auto-generated constructor stub
}
@Override
public void function() {
super.function();
System.out.println("添加额外功能02:Rap");
}
}
Decorator3类:
package decorator.DIYDecorator;
//虽然没有继承Decorator,但是仍按照装饰器的要求进行书写
public class Decorator3 implements Person{
private Person person;
public Decorator3(Person person) {
this.person=person;
}
@Override
public void function() {
person.function();
System.out.println("添加额外功能3:打篮球");
if(person instanceof CaiXuKun) {
System.out.println("===触发终极技能:鸡你太美===");
}
}
}
Test测试:
package decorator.DIYDecorator;
public class Test {
public static void main(String[] args) {
System.out.println("========装饰器能对实现Person接口的'非装饰器类型的任意类'进行装饰、反复装饰========");
System.out.println("==========你可以对CaiXuKun类装饰,也可以对Singer类进行装饰============");
// Person p = new Xxx();
// Person p = new Singer();
Person p = new CaiXuKun();
p.function();
System.out.println("========Decorator01装饰后=========");
Person p_Dec01 = new Decorator01(p);
p_Dec01.function();
System.out.println("========Decorator02装饰后=========");
Person p_Dec02_Dec01 = new Decorator02(p_Dec01);
p_Dec02_Dec01.function();
System.out.println("========Decorator02单独对p装饰后=========");
Person p_Dec02=new Decorator02(p);
p_Dec02.function();
System.out.println("======Decorator3并未继承Decorator,但仍按照其规范书写======");
System.out.println("========Decorator3单独对p装饰后=========");
Person p_Dec3 = new Decorator3(p);
p_Dec3.function();
Person p_Dec01_Dec02_Dec3 = new Decorator3(new Decorator02(new Decorator01(p)));
System.out.println("========Decorator01,02,3依次对p装饰后=========");
p_Dec01_Dec02_Dec3.function();
System.out.println("这里之所以没有触发鸡你太美,是因为触发条件是:当前对象的Person成员变量指向CaiXuKun对象"
+ "而p_Dec01_Dec02_Dec3的当前对象是Decorator3,它的成员变量Person指向Decorator02"
+ "Decorator02的成员变量Person指向Decorator01,Decorator01的成员变量Person才指向CaiXuKun对象");
}
}
测试结果:
========装饰器能对实现Person接口的'非装饰器类型的任意类'进行装饰、反复装饰========
==========你可以对CaiXuKun类装饰,也可以对Singer类进行装饰============
CaiXuKun能唱歌
========Decorator01装饰后=========
CaiXuKun能唱歌
添加额外功能01:跳舞
========Decorator02装饰后=========
CaiXuKun能唱歌
添加额外功能01:跳舞
添加额外功能02:Rap
========Decorator02单独对p装饰后=========
CaiXuKun能唱歌
添加额外功能02:Rap
======Decorator3并未继承Decorator,但仍按照其规范书写======
========Decorator3单独对p装饰后=========
CaiXuKun能唱歌
添加额外功能3:打篮球
===触发终极技能:鸡你太美===
========Decorator01,02,3依次对p装饰后=========
CaiXuKun能唱歌
添加额外功能01:跳舞
添加额外功能02:Rap
添加额外功能3:打篮球
这里之所以没有触发鸡你太美,是因为触发条件是:当前对象的Person成员变量指向CaiXuKun对象而p_Dec01_Dec02_Dec3的当前对象是Decorator3,它的成员变量Person指向Decorator02Decorator02的成员变量Person指向Decorator01,Decorator01的成员变量Person才指向CaiXuKun对象