《Head First设计模式》阅读笔记.第七章-外观模式实例补充
来源:百度文库 编辑:神马文学网 时间:2024/04/29 04:48:21
2010-01-21
这个是测试程序:
Java代码
测试结果:
引用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.
《Head First设计模式》阅读笔记.第七章-外观模式实例补充
文章分类:Java编程 Java代码- // 电源
- public class Power {
- public void connect() {
- System.out.println("The power is connected.");
- }
- public void disconnect() {
- System.out.println("The power is disconnected.");
- }
- }
- // 主板
- public class MainBoard {
- public void on() {
- System.out.println("The mainboard is on.");
- }
- public void off() {
- System.out.println("The mainboard is off.");
- }
- }
- // 硬盘
- public class HardDisk {
- public void run() {
- System.out.println("The harddisk is running.");
- }
- public void stop() {
- System.out.println("The harddisk is stopped.");
- }
- }
- // 操作系统
- public class OperationSystem {
- public void startup() {
- System.out.println("The opertion system is startup.");
- }
- public void shutdown() {
- System.out.println("The operation system is shutdown.");
- }
- }
- // 计算机外观
- public class Computer {
- private Power power;
- private MainBoard board;
- private HardDisk disk;
- private OperationSystem system;
- public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {
- this.power = power;
- this.board = board;
- this.disk = disk;
- this.system = system;
- }
- public void startup() {
- this.power.connect();
- this.board.on();
- this.disk.run();
- this.system.startup();
- }
- public void shutdown() {
- this.system.shutdown();
- this.disk.stop();
- this.board.off();
- this.power.disconnect();
- }
- }
// 电源public class Power {public void connect() {System.out.println("The power is connected.");}public void disconnect() {System.out.println("The power is disconnected.");}}// 主板public class MainBoard {public void on() {System.out.println("The mainboard is on.");}public void off() {System.out.println("The mainboard is off.");}}// 硬盘public class HardDisk {public void run() {System.out.println("The harddisk is running.");}public void stop() {System.out.println("The harddisk is stopped.");}}// 操作系统public class OperationSystem {public void startup() {System.out.println("The opertion system is startup.");}public void shutdown() {System.out.println("The operation system is shutdown.");}}// 计算机外观public class Computer {private Power power;private MainBoard board;private HardDisk disk;private OperationSystem system;public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {this.power = power;this.board = board;this.disk = disk;this.system = system;}public void startup() {this.power.connect();this.board.on();this.disk.run();this.system.startup();}public void shutdown() {this.system.shutdown();this.disk.stop();this.board.off();this.power.disconnect();}}
这个是测试程序:
Java代码
- // 测试计算机外观类
- public class TestComputer {
- public static void main(String[] args) {
- Power power = new Power();
- MainBoard board = new MainBoard();
- HardDisk disk = new HardDisk();
- OperationSystem system = new OperationSystem();
- Computer computer = new Computer(power, board, disk, system);
- computer.startup();
- computer.shutdown();
- }
- }
// 测试计算机外观类public class TestComputer {public static void main(String[] args) {Power power = new Power();MainBoard board = new MainBoard();HardDisk disk = new HardDisk();OperationSystem system = new OperationSystem();Computer computer = new Computer(power, board, disk, system);computer.startup();computer.shutdown();}}
测试结果:
引用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.
《Head First设计模式》阅读笔记.第七章-外观模式实例补充
《Head First设计模式》阅读笔记.第七章
《Head First设计模式》阅读笔记.第一章
《Head First设计模式》阅读笔记.第十三章
设计模式&重整技术运用实例
设计模式&重整技术运用实例
《Java与模式》学习笔记:设计模式——树结构(合成模式 模板模式)
《Java与模式》学习笔记:设计模式——树结构(合成模式 模板模式) - shanchao...
深入浅出面向对象分析与设计(中文版)(Head First系列)
《Java与模式》学习笔记:设计模式——抽象工厂模式 - shanchao的专栏 - CS...
《Java与模式》学习笔记:设计模式——模板(Template)模式
《Java与模式》学习笔记:设计模式——模板(Template)模式 - shanchao...
设计模式
设计模式
设计模式
设计模式
设计模式学习笔记(一)——面向对象设计模式与原则
设计模式学习笔记(七)——Adapter适配器模式
设计模式学习笔记(八)——Bridge桥接模式
设计模式学习笔记(十四)——创建型模式总结
《Java与模式》学习笔记:设计模式——原则 - shanchao的专栏 - CSDNBl...
设计模式学习笔记(十四)——创建型模式总结
设计模式之结构模式
《java与模式》笔记