《Head First设计模式》阅读笔记.第七章-外观模式实例补充

来源:百度文库 编辑:神马文学网 时间:2024/04/29 04:48:21
2010-01-21

《Head First设计模式》阅读笔记.第七章-外观模式实例补充

文章分类:Java编程 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.