外观模式
对一系列接口进行二次封装,供上层使用。上层使用时只需按照规则进行即可。
通过中间者让客户端和子系统解耦,也让子系统更易于扩展
image.png
Client.js
const Computer = require('./Computer');
computer = new Computer();
computer.start();
computer.shutDown();
Computer.js
const CPU = require('./CPU');
const Memory = require('./Memory');
const Disk = require('./Disk');
function Computer() {
let _cpu, _memory, _disk;
_cpu = new CPU();
_memory = new Memory();
_disk = new Disk();
this.start = function () {
_cpu.startUp();
_memory.startUp();
_disk.startUp();
};
this.shutDown = function() {
_cpu.shutDown();
_memory.shutDown();
_disk.shutDown();
};
}
module.exports = Computer;
CPU.js
function CPU() {
this.startUp = function() {
console.log("CPU运行");
};
this.shutDown = function() {
console.log("CPU停止");
};
}
module.exports = CPU;
Disk.js
function Disk() {
this.startUp = function() {
console.log("Disk运行");
};
this.shutDown = function() {
console.log("Disk停止");
};
}
module.exports = Disk;
Memory.js
function Memory() {
this.startUp = function() {
console.log("Memory运行");
};
this.shutDown = function() {
console.log("Memory停止");
};
}
module.exports = Memory;
运行结果
网友评论