Middle+
What design pattern have we just implemented? Have you consciously applied design patterns in your work?
sobes.tech AI
Answer from AI
Design patterns are often used in frontend development to improve the structure and maintainability of code. For example, if we implement the "Observer" pattern, it means we have a publisher object that notifies subscribers about changes in state.
In my practice, I consciously used patterns such as MVC for separating logic and presentation, Singleton for managing global states, and Factory for creating objects with different configurations. This helps make the code more readable, extendable, and easier to maintain.
An example of the Observer pattern in JavaScript:
class Subject {
constructor() {
this.observers = [];
}
subscribe(observer) {
this.observers.push(observer);
}
notify(data) {
this.observers.forEach(observer => observer.update(data));
}
}
class Observer {
update(data) {
console.log('Received data:', data);
}
}
const subject = new Subject();
const observer = new Observer();
subject.subscribe(observer);
subject.notify('New message');