js设计模式——8.中介者模式
/*js设计模式——中介者模式*/ class A {
constructor() {
this.number = 0;
} setNumber(num, m) {
this.number = num;
if (m) {
m.setB();
}
}
} class B {
constructor() {
this.number = 0;
} setNumber(num, m) {
this.number = num;
if (m) {
m.setA();
}
}
} // 中介者
class MediatorDome {
constructor(a, b) {
this.a = a;
this.b = b;
} setB() {
let number = this.a.number;
this.b.setNumber(number * 100);
} setA() {
let number = this.b.number;
this.b.setNumber(number / 100);
}
} // 测试
let a = new A();
let b = new B();
let m = new MediatorDome(a, b);
a.setNumber(100, m);
console.log(a.number, b.number);
a.setNumber(100, m);
console.log(a.number, b.number);