装饰者模式
2016-01-03 20:54
281 查看
创建抽象装饰类Decorator,包含一个MobilePhone类型的私有变量。
1 public class Decorator extends MobilePhone{ 2 3 private MobilePhone _mobilePhone; 4 5 public Decorator(MobilePhone mobilePhone){ 6 _mobilePhone=mobilePhone; 7 phoneName=mobilePhone.phoneName; 8 } 9 @Override 10 public void SendMessage() { 11 // TODO Auto-generated method stub 12 _mobilePhone.SendMessage(); 13 } 14 15 @Override 16 public void Call() { 17 // TODO Auto-generated method stub 18 _mobilePhone.Call(); 19 } 20 21 } 书写主函数Main来分别创建小米手机和苹果手机,并且分别加上蓝牙功能、GPS功能和视频通话功能。
1 public class Main { 2 3 public static void main(String[] args) { 4 // TODO Auto-generated method stub 5 MiPhone miPhone=new MiPhone(); 6 iPhone iphone=new iPhone(); 7 8 Bluetooth miBluetooth=new Bluetooth(miPhone); 9 miBluetooth.Connect(); 10 GPS miGPS=new GPS(miPhone); 11 miGPS.Location="MiPhone的定位成功"; 12 System.out.println(miGPS.Location); 13 Camera miCamera=new Camera(miPhone); 14 miCamera.VideoCall(); 15 16 Bluetooth iBluetooth=new Bluetooth(iphone); 17 iBluetooth.Connect(); 18 GPS iGPS=new GPS(iphone); 19 miGPS.Location="iPhone的定位成功"; 20 System.out.println(miGPS.Location); 21 Camera iCamera=new Camera(iphone); 22 iCamera.VideoCall(); 23 } 24 25 }
相关文章推荐
- 回车和换行的区别
- Positioned Update not supported异常处理
- Tomcat中的设计模式--外观模式(门店模式)
- Django如何搭建服务器
- sudo 扩展学习
- 2016 目标清单
- 算法导论学习---红黑树具体解释之插入(C语言实现)
- mysql
- su 和 sudo 命令的区别
- 任正非“互联网思维”如是说
- 状态模式
- 状态者模式
- 【Java EE 学习 82 下】【MAVEN整合Eclipse】【MAVEN的一些高级概念】
- linux 使用fdisk分区扩容
- 初学java感想
- 验证尼科彻斯定理
- jquery选择asp:RadioButtonList的选择项
- mysql安装
- Memcache+Tomcat9集群实现session共享(非jar式配置, 手动编写Memcache客户端)
- Linux中find命令-path -prune用法详解