[实验任务一]:手机功能的升级
用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人;而JarPhone除了声音还能振动;更高级的手机(ComplexPhone)除了声音、振动外,还有灯光闪烁提示。
实验要求:
1. 提交类图;
2. 提交源代码;
package test11;
public abstract class Phone {
public void serviceCall() {
}
}
package test11;
public class ComplexPhone extends PhoneDecoration {
public ComplexPhone(Phone phone) {
super(phone);
}
@Override
public void serviceCall() {
super.serviceCall();
System.out.println("ComplexPhone:发出声音、震动并灯光闪烁提醒主人电话来了");
}
}
package test11;
public class PhoneDecoration extends Phone {
private Phone phone = null;
public PhoneDecoration(Phone phone) {
if (phone != null) {
this.phone = phone;
} else {
this.phone = new SimplePhone();
}
}
@Override
public void serviceCall() {
phone.serviceCall();
}
}
package test11;
public class Client {
public static void main(String[] args) {
Phone p1 = new SimplePhone();
System.out.println("生产简单手机");
p1.serviceCall();
System.out.println("***********************");
Phone p2 = new JarPhone(p1);
System.out.println("升级为JarPhone手机");
p2.serviceCall();
System.out.println("***********************");
Phone p3 = new ComplexPhone(p2);
System.out.println("升级为ComplexPhone手机");
p3.serviceCall();
}
}
package test11;
public class JarPhone extends PhoneDecoration {
public JarPhone(Phone phone) {
super(phone);
}
@Override
public void serviceCall() {
super.serviceCall();
System.out.println("JarPhone:发出声音并震动提醒主人电话来了");
}
}
package test11;
public class SimplePhone extends Phone {
public void serviceCall() {
System.out.println("SimplePhone:发出声音提醒主人电话来了");
}
}
3.注意编程规范。