打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
《Head First设计模式》阅读笔记.第七章-外观模式实例补充
Java代码
  1. // 电源   
  2. public class Power {   
  3.     public void connect() {   
  4.         System.out.println("The power is connected.");   
  5.     }   
  6.   
  7.     public void disconnect() {   
  8.         System.out.println("The power is disconnected.");   
  9.     }   
  10. }   
  11.   
  12. // 主板   
  13. public class MainBoard {   
  14.     public void on() {   
  15.         System.out.println("The mainboard is on.");   
  16.     }   
  17.   
  18.     public void off() {   
  19.         System.out.println("The mainboard is off.");   
  20.     }   
  21. }   
  22.   
  23. // 硬盘   
  24. public class HardDisk {   
  25.     public void run() {   
  26.         System.out.println("The harddisk is running.");   
  27.     }   
  28.   
  29.     public void stop() {   
  30.         System.out.println("The harddisk is stopped.");   
  31.     }   
  32. }   
  33.   
  34. // 操作系统   
  35. public class OperationSystem {   
  36.     public void startup() {   
  37.         System.out.println("The opertion system is startup.");   
  38.     }   
  39.   
  40.     public void shutdown() {   
  41.         System.out.println("The operation system is shutdown.");   
  42.     }   
  43. }   
  44.   
  45. // 计算机外观   
  46. public class Computer {   
  47.     private Power power;   
  48.   
  49.     private MainBoard board;   
  50.   
  51.     private HardDisk disk;   
  52.   
  53.     private OperationSystem system;   
  54.   
  55.     public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {   
  56.         this.power = power;   
  57.         this.board = board;   
  58.         this.disk = disk;   
  59.         this.system = system;   
  60.     }   
  61.   
  62.     public void startup() {   
  63.         this.power.connect();   
  64.         this.board.on();   
  65.         this.disk.run();   
  66.         this.system.startup();   
  67.     }   
  68.   
  69.     public void shutdown() {   
  70.         this.system.shutdown();   
  71.         this.disk.stop();   
  72.         this.board.off();   
  73.         this.power.disconnect();   
  74.     }   
  75. }  


这个是测试程序:
Java代码
  1. // 测试计算机外观类   
  2. public class TestComputer {   
  3.     public static void main(String[] args) {   
  4.         Power power = new Power();   
  5.         MainBoard board = new MainBoard();   
  6.         HardDisk disk = new HardDisk();   
  7.         OperationSystem system = new OperationSystem();   
  8.   
  9.         Computer computer = new Computer(power, board, disk, system);   
  10.         computer.startup();   
  11.         computer.shutdown();   
  12.     }   
  13. }  


测试结果:
引用
The power is connected.
The mainboard is on.
The harddisk is running.
The opertion system is startup.
The operation system is shutdown.
The harddisk is stopped.
The mainboard is off.
The power is disconnected.
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
UML中类之间的关系
求!!关于用java判断输入3个数组成什么三角形
研磨设计模式之 命令模式-1
超详细解读Java接口:模块通信协议以及默认方法和静态方法
Java设计模式-软件设计原则
软件设计原则讲解,昭昭在目
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服