11.4日报
今天完成了设计模式的实验十一,以下为实验内容:
实验11:装饰模式 本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解装饰模式的动机,掌握该模式的结构; 2、能够利用装饰模式解决实际问题。 [实验任务一]:手机功能的升级 用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人;而JarPhone除了声音还能振动;更高级的手机(ComplexPhone)除了声音、振动外,还有灯光闪烁提示。 实验要求: 1. 提交类图; 2.提交源代码; // 抽象组件 interface Phone { void ring(); } // 具体组件 class SimplePhone implements Phone { @Override public void ring() { System.out.println("SimplePhone is ringing."); } } // 抽象装饰者 abstract class Decorator implements Phone { protected Phone phone; public Decorator(Phone phone) { this.phone = phone; } @Override public void ring() { phone.ring(); } } // 具体装饰者1:JarPhone class JarPhone extends Decorator { public JarPhone(Phone phone) { super(phone); } @Override public void ring() { super.ring(); System.out.println("JarPhone is vibrating."); } } // 具体装饰者2:ComplexPhone class ComplexPhone extends Decorator { public ComplexPhone(Phone phone) { super(phone); } @Override public void ring() { super.ring(); System.out.println("ComplexPhone is flashing lights."); } } // 客户端代码 public class DecoratorPatternDemo { public static void main(String[] args) { Phone phone = new SimplePhone(); System.out.println("SimplePhone:"); phone.ring(); System.out.println("\nJarPhone:"); Phone jarPhone = new JarPhone(phone); jarPhone.ring(); System.out.println("\nComplexPhone:"); Phone complexPhone = new ComplexPhone(phone); complexPhone.ring(); } } 3.注意编程规范。

浙公网安备 33010602011771号