自适应网站设计案例,求推荐专业的网站建设开发,宁波专业网站建设模板服务,网站建设源代码什么是观察者设计模式
观察者模式#xff08;Observer Pattern#xff09;是前端开发中常用的一种设计模式。它定义了一种一对多的依赖关系#xff0c;使得当一个对象的状态发生改变时#xff0c;其所有依赖对象都能收到通知并自动更新。观察者模式广泛应用于事件驱动的系…什么是观察者设计模式
观察者模式Observer Pattern是前端开发中常用的一种设计模式。它定义了一种一对多的依赖关系使得当一个对象的状态发生改变时其所有依赖对象都能收到通知并自动更新。观察者模式广泛应用于事件驱动的系统如浏览器事件、发布-订阅系统以及各类框架的状态管理机制。
观察者设计模式定义了一种 一对多 的依赖关系当 一个对象的状态发生变化 时所有依赖它的对象都会自动收到通知。这种模式可以解耦观察者和被观察者使得它们可以独立变化。
观察者模式的关键角色
Subject被观察者: 负责维护观察者列表并在自身状态发生变化时通知所有观察者。Observer观察者: 订阅目标的变化并在收到通知时执行相关操作。
观察者模式的实现
1. 简单观察者模式
// 观察者接口
interface Observer {update(state: string): void;
}// 被观察者
class Subject {private observers: Observer[] [];private state: string ;// 添加观察者public attach(observer: Observer): void {this.observers.push(observer);}// 移除观察者public detach(observer: Observer): void {this.observers this.observers.filter(obs obs ! observer);}// 通知所有观察者public notify(): void {this.observers.forEach(observer observer.update(this.state));}// 改变状态并通知观察者public setState(state: string): void {this.state state;this.notify();}
}// 具体的观察者
class ConcreteObserver implements Observer {private name: string;constructor(name: string) {this.name name;}// 收到通知时的动作public update(state: string): void {console.log(${this.name} received update: ${state});}
}// 测试观察者模式
const subject new Subject();const observer1 new ConcreteObserver(Observer 1);
const observer2 new ConcreteObserver(Observer 2);subject.attach(observer1);
subject.attach(observer2);subject.setState(State A);
subject.setState(State B);在这个例子中我们实现了基本的观察者模式一个 Subject 类可以被多个 Observer 观察每当 Subject 的状态发生变化时它会通知所有观察者。
2. 发布-订阅模式
发布-订阅模式Publish-Subscribe Pattern 是观察者模式的一种变体通常通过事件系统来实现。发布者不直接通知观察者而是通过中介消息中心将事件广播出去由订阅者选择性地接收。
// 消息中心
class EventEmitter {private events: { [key: string]: Function[] } {};// 订阅事件public subscribe(event: string, listener: Function): void {if (!this.events[event]) {this.events[event] [];}this.events[event].push(listener);}// 发布事件public publish(event: string, data?: any): void {if (this.events[event]) {this.events[event].forEach(listener listener(data));}}// 取消订阅public unsubscribe(event: string, listener: Function): void {if (this.events[event]) {this.events[event] this.events[event].filter(l l ! listener);}}
}// 测试发布-订阅模式
const eventEmitter new EventEmitter();const handler (data: any) console.log(Event received:, data);// 订阅事件
eventEmitter.subscribe(eventA, handler);// 发布事件
eventEmitter.publish(eventA, { message: Hello, World! });// 取消订阅
eventEmitter.unsubscribe(eventA, handler);在发布-订阅模式中发布者和订阅者是通过 EventEmitter 消息中心进行通信的发布者无需知道订阅者的存在这使得它们之间的耦合度进一步降低。
3. 双向绑定模式MVVM
MVVMModel-View-ViewModel 是一种常见的双向绑定模式在前端框架如 Vue、Angular 中很常见。它通过数据绑定使得 View 和 Model 可以双向通信通常是通过一个 ViewModel 来桥接二者。
// ViewModel
class ViewModel {private state: string ;private observers: Function[] [];// 获取当前状态public getState(): string {return this.state;}// 设置状态并通知视图更新public setState(newState: string): void {this.state newState;this.notify();}// 添加视图更新的回调public bind(observer: Function): void {this.observers.push(observer);}// 通知视图更新private notify(): void {this.observers.forEach(observer observer(this.state));}
}// 视图更新函数
const updateView (state: string) {console.log(View updated:, state);
};// 测试双向绑定
const viewModel new ViewModel();
viewModel.bind(updateView);viewModel.setState(New State);
console.log(Current State:, viewModel.getState());在这个例子中ViewModel 充当了模型和视图之间的桥梁确保视图在数据更新时及时反应。这是 MVVM 模式的一个简单实现。
总结
观察者设计模式及其变体在前端开发中起着至关重要的作用。通过观察者模式可以解耦对象之间的直接依赖关系从而提升系统的灵活性和可维护性。无论是基础的观察者模式还是更为复杂的发布-订阅模式、双向绑定模式都在不同的场景中发挥了不可替代的作用。 文章转载自: http://www.morning.nyjgm.cn.gov.cn.nyjgm.cn http://www.morning.tqfnf.cn.gov.cn.tqfnf.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.wpmqq.cn.gov.cn.wpmqq.cn http://www.morning.rhkmn.cn.gov.cn.rhkmn.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.yzfrh.cn.gov.cn.yzfrh.cn http://www.morning.wttzp.cn.gov.cn.wttzp.cn http://www.morning.yqndr.cn.gov.cn.yqndr.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.brwnd.cn.gov.cn.brwnd.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.tgbx.cn.gov.cn.tgbx.cn http://www.morning.yxnfd.cn.gov.cn.yxnfd.cn http://www.morning.qfbzj.cn.gov.cn.qfbzj.cn http://www.morning.mczjq.cn.gov.cn.mczjq.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.dkqr.cn.gov.cn.dkqr.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.ppllj.cn.gov.cn.ppllj.cn http://www.morning.ktlfb.cn.gov.cn.ktlfb.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.pmptm.cn.gov.cn.pmptm.cn http://www.morning.ctxt.cn.gov.cn.ctxt.cn http://www.morning.krgjc.cn.gov.cn.krgjc.cn http://www.morning.mrncd.cn.gov.cn.mrncd.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.dspqc.cn.gov.cn.dspqc.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.gtbjc.cn.gov.cn.gtbjc.cn http://www.morning.bnygf.cn.gov.cn.bnygf.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.clkjn.cn.gov.cn.clkjn.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.gqmhq.cn.gov.cn.gqmhq.cn http://www.morning.lsssx.cn.gov.cn.lsssx.cn http://www.morning.zdzgf.cn.gov.cn.zdzgf.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.xnnxp.cn.gov.cn.xnnxp.cn http://www.morning.hbdqf.cn.gov.cn.hbdqf.cn http://www.morning.pqkyx.cn.gov.cn.pqkyx.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.mrfbp.cn.gov.cn.mrfbp.cn http://www.morning.dzqr.cn.gov.cn.dzqr.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.phlwj.cn.gov.cn.phlwj.cn http://www.morning.hrzymy.com.gov.cn.hrzymy.com http://www.morning.wrdlf.cn.gov.cn.wrdlf.cn http://www.morning.npgwb.cn.gov.cn.npgwb.cn http://www.morning.kjgrg.cn.gov.cn.kjgrg.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.trjdr.cn.gov.cn.trjdr.cn http://www.morning.fstdf.cn.gov.cn.fstdf.cn http://www.morning.mkfhx.cn.gov.cn.mkfhx.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.kfldw.cn.gov.cn.kfldw.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.ddxjr.cn.gov.cn.ddxjr.cn http://www.morning.rqkzh.cn.gov.cn.rqkzh.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.zfqdt.cn.gov.cn.zfqdt.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn