Junior — Middle
Can you name the main design patterns used in software development?
sobes.tech AI
Answer from AI
Main design patterns commonly used in software development:
- Singleton — ensures that a class has only one instance and provides a global point of access to it.
- Factory Method — defines an interface for creating an object but allows subclasses to alter the type of objects that will be created.
- Observer — allows objects to subscribe to events of another object and receive notifications about changes.
- Decorator — dynamically adds responsibilities to objects by wrapping them.
- Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.
- Adapter — allows objects with incompatible interfaces to work together.
Example of Singleton in JavaScript:
class Singleton {
constructor() {
if (Singleton.instance) {
return Singleton.instance;
}
this.value = Math.random();
Singleton.instance = this;
}
}
const a = new Singleton();
const b = new Singleton();
console.log(a === b); // true