All quizzesHard
Architectural Patterns
Preview — 3 of 10 questions
What design problem does this factory pattern solve?
javascript
class ReportFactory {
static registeredReports = {};
static register(type, ReportClass) {
this.registeredReports[type] = ReportClass;
}
static create(type) {
const ReportClass = this.registeredReports[type];
if (!ReportClass) {
throw new Error(`Report type '${type}' not registered`);
}
return new ReportClass();
}
}
// Third-party plugin can register new report types
ReportFactory.register("pdf", PDFReport);
ReportFactory.register("excel", ExcelReport);
// Later, third-party code registers new type without modifying factory
ReportFactory.register("custom", CustomReport);
const report = ReportFactory.create("custom");AAllows extending factory with new types without modifying existing code (Open/Closed Principle)
BPrevents new report types from being added
CCreates a singleton factory
DOptimizes report creation performance
In an MVC architecture, which patterns are typically combined?
javascript
// Model (subject)
class UserModel {
constructor() {
this.observers = [];
this.user = {};
}
subscribe(observer) {
this.observers.push(observer);
}
setUser(user) {
this.user = user;
this.notifyObservers(); // Observer pattern
}
notifyObservers() {
this.observers.forEach(obs => obs.update(this.user));
}
}
// View
class UserView {
update(user) {
console.log(`User: ${user.name}`);
}
}
// Controller
class UserController {
constructor(model, view) {
this.model = model;
this.view = view;
this.model.subscribe(this.view);
}
handleUserUpdate(user) {
this.model.setUser(user);
}
}
// Factory (creates MVC components)
class MVCFactory {
static createMVC() {
const model = new UserModel();
const view = new UserView();
const controller = new UserController(model, view);
return { model, view, controller };
}
}
// Usage
const { model, view, controller } = MVCFactory.createMVC();
controller.handleUserUpdate({ name: "Alice" }); // View automatically updatesAObserver, Factory, and Strategy patterns working together
BOnly Singleton pattern
COnly Adapter pattern
DDecorator and Proxy patterns
What problem does the Facade pattern solve when combined with many underlying patterns?
javascript
// Client must understand all these patterns
const factory = new ReportFactory();
const strategy = new PdfStrategy();
const report = factory.create("pdf");
report.setStrategy(strategy);
report.attach(observer);
report.build();
const result = report.generate();APrevents patterns from conflicting
BPrevents pattern usage altogether
CMakes patterns faster
DSimplifies a complex system with many interacting patterns
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.