2. 在电脑主机(Mainframe)中,只需要按下主机的开机按钮(on()),即可调用其他硬件设备和软件的启动方法,如内存(Memory)的自检(check()),CPU的运行(run()),硬盘(HardDisk)的读取(read()),操作系统(OS)的载入(load())等,如果某一过程发生错误则电脑启动失败。使用外观模式模拟该过程,绘制类图并编程模拟实现。
时间: 2023-08-23 10:06:04 浏览: 57
好的,我知道了。下面是类图和代码实现:
**类图:**
```
+------------------+ +-----------------------+
| MainframeFacade | | Mainframe |
+------------------+ +-----------------------+
| +on() | | +check() |
| | 依赖 | +run() |
| | -------->| +read() |
| | | +load() |
+------------------+ +-----------------------+
| | | HardDisk |
| | +-----------------------+
| | | +read() |
| | +-----------------------+
| | | Memory |
| | +-----------------------+
| | | +check() |
| | +-----------------------+
| | | CPU |
| | +-----------------------+
| | | +run() |
| | +-----------------------+
| | | OperatingSystem(OS) |
| | +-----------------------+
| | | +load() |
| | +-----------------------+
```
**代码实现:**
```python
# Mainframe 中包含了各个硬件的启动方法
class Mainframe:
def check(self):
print('Memory checking...')
# 内存自检
return True
def run(self):
print('CPU running...')
# CPU 运行
return True
def read(self):
print('HardDisk reading...')
# 硬盘读取
return True
def load(self):
print('Operating System loading...')
# 操作系统载入
return True
# 外观类 MainframeFacade 简化了启动过程
class MainframeFacade:
def __init__(self):
self.mainframe = Mainframe()
def on(self):
# 按下主机的开机按钮,启动电脑
if not self.mainframe.check():
print('Failed to start! Memory error.')
elif not self.mainframe.run():
print('Failed to start! CPU error.')
elif not self.mainframe.read():
print('Failed to start! HardDisk error.')
elif not self.mainframe.load():
print('Failed to start! Operating System error.')
else:
print('Successfully started the computer!')
# 其他硬件设备和软件
class HardDisk:
def read(self):
print('HardDisk reading...')
# 硬盘读取
return True
class Memory:
def check(self):
print('Memory checking...')
# 内存自检
return True
class CPU:
def run(self):
print('CPU running...')
# CPU 运行
return True
class OperatingSystem:
def load(self):
print('Operating System loading...')
# 操作系统载入
return True
```
调用示例:
```python
computer = MainframeFacade()
computer.on()
```
输出:
```
Memory checking...
CPU running...
HardDisk reading...
Operating System loading...
Successfully started the computer!
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)