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

// 电源
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 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();
}
}


测试结果:
[quote]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.[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值