设计模式之外观模式
结构
说明
Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.
为子系统中的一组接口提供一个一致的界面, 外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
适用条件
- 为一个比较复杂的子系统提供一个简单的接口;
- 将客户程序与子系统的实现部分分离, 提高子系统的独立性和可移植性;
- 简化子系统间的依赖关系。
实现
代码语言:javascript复制class Cpu {
public void Freeze() {
}
public void Jump(long position) {
}
public void Execute() {
}
}
class Memory {
public void Load(long address, byte[] data) {
}
}
class HardDrive {
public byte[] Read(long address) {
return new byte[0];
}
}
class Computer {
private readonly Cpu _cpu;
private readonly Memory _memory;
private readonly HardDrive _hardDrive;
private const long BootAddress = 0;
public Computer() {
this._cpu = new Cpu();
this._memory = new Memory();
this._hardDrive = new HardDrive();
}
public void StartComputer() {
this._cpu.Freeze();
this._memory.Load(BootAddress, this._hardDrive.Read( BootAddress));
this._cpu.Execute();
}
}
class Program {
static void Main(string[] args) {
var facade = new Computer();
facade.StartComputer();
}
}