2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Design patterns are general solutions to common problems in software design. Here are some common design patterns, divided into three categories: creational patterns, structural patterns, and behavioral patterns.
These patterns provide mechanisms for object creation, increasing the flexibility and reusability of existing code.
Singleton:
Factory Method:
Abstract Factory:
Builder:
Prototype:
These patterns deal with object composition, or objects and the relationships between them.
Adapter:
Decorator pattern:
Proxy:
Facade:
Bridge Mode:
Composite:
Flyweight:
These patterns focus on communication between objects, that is, how objects interact with each other and how responsibilities are distributed.
Strategy:
Template Method:
Observer:
Iterator:
Chain of Responsibility:
Command:
Memento:
State:
Visitor pattern:
Mediator:
Interpreter:
During my last interview, I was asked to implement the subscription-publisher pattern with code. I have time to simulate these design patterns with js code.
For example
class PubSub {
constructor() {
this.events = {}; // 存储事件名称和对应的订阅者回调函数数组
}
// 订阅事件
subscribe(event, callback) {
if (!this.events[event]) {
this.events[event] = []; // 如果事件不存在,初始化一个空数组
}
this.events[event].push(callback); // 将回调函数添加到订阅者的数组
}
// 取消订阅事件
unsubscribe(event, callback) {
if (!this.events[event]) {
return;
}
this.events[event] = this.events[event].filter(cb => cb !== callback); // 移除指定的回调函数
}
// 取消特定事件的所有订阅
unsubscribeAll(event) {
if (this.events[event]) {
delete this.events[event]; // 删除所有订阅者
}
}
// 触发事件,使用 emit 作为方法名
emit(event, data) {
if (this.events[event]) {
// 执行所有订阅者的回调函数
this.events[event].forEach(callback => callback(data));
}
}
// 检查是否有订阅者
hasSubscribers(event) {
return this.events[event] && this.events[event].length > 0;
}
}
// 使用示例
const eventCenter = new PubSub();
// 订阅 'message' 事件
eventCenter.subscribe('message', (data) => {
console.log(`Message received: ${data}`);
});
// 订阅 'greet' 事件
eventCenter.subscribe('greet', (name) => {
console.log(`Hello, ${name}!`);
});
// 触发 'message' 事件
eventCenter.emit('message', 'Hello, Pub/Sub!');
// 触发 'greet' 事件
eventCenter.emit('greet', 'World');
// 取消对 'message' 事件的订阅
const myCallback = (data) => {
console.log(`My callback received: ${data}`);
};
eventCenter.subscribe('message', myCallback);
eventCenter.unsubscribe('message', myCallback);
// 再次触发 'message' 事件,myCallback 不会被调用
eventCenter.emit('message', 'This message will not be received by myCallback');
In this example,PubSub
The class provides the following functionality:
subscribe
The method allows a subscriber to register a callback function to a specific event.unsubscribe
Method allows a subscriber to unregister its callback function from a specific event.unsubscribeAll
Method cancels all subscriptions to a specific event.emit
The method triggers an event and executes the callback functions of all subscribers.hasSubscribers
Method checks if there are any subscribers for a specific event.