用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人;而JarPhone除了声音还能振动;更高级的手机(ComplexPhone)除了声音、振动外,还有灯光闪烁提示。
实验要求:
1. 提交类图;
2.提交源代码;
package Decorative_pattern;
public class Client {
public static void main(String[]args) {
Phone p=new SimplePhone();
p.call();
System.out.println("Simple");
JarPhone p1=new JarPhone(p);
p1.shoke();
System.out.println("JarPhone");
ComplexPhone p2=new ComplexPhone(p1);
p2.light();
System.out.println("ComplexPhone");
}
}
package Decorative_pattern;
public class ComplexPhone extends UpgradePhone{
public ComplexPhone(Phone phone){
super(phone);
System.out.println("***增加闪光功能***");
}
public void light(){
System.out.println("灯光闪烁:咔嚓");
}
}
package Decorative_pattern;
public class JarPhone extends UpgradePhone{
public JarPhone(Phone phone){
super(phone);
System.out.println("***增加振动的功能***");
}
public void shoke(){
System.out.println("振动:嗡嗡嗡");
}
}
package Decorative_pattern;
public class SimplePhone implements Phone{
public SimplePhone(){
System.out.println("手机收到来电");
}
public void call(){
System.out.println("主人来电话啦!");
}
}
package Decorative_pattern;
public class UpgradePhone implements Phone{
private Phone phone;
public UpgradePhone(Phone a){
phone=a;
}
public void call(){
phone.call();
}
}
package Decorative_pattern;
public interface Phone {
public void call();
}
3.注意编程规范。
标签:phone,软件设计,番外,System,Phone,println,public,out From: https://www.cnblogs.com/zjsdbk/p/17819446.html