All quizzesHard
Behavioral Patterns — Series 2
Preview — 3 of 10 questions
What pattern does ChatRoom play the role of here, and what is logged?
javascript
class ChatRoom {
showMessage(user, message) {
return `[${user}]: ${message}`;
}
}
class User {
constructor(name, chatRoom) {
this.name = name;
this.chatRoom = chatRoom;
}
send(message) {
return this.chatRoom.showMessage(this.name, message);
}
}
const room = new ChatRoom();
const alice = new User('Alice', room);
const bob = new User('Bob', room);
console.log(alice.send('Hi Bob!'));
console.log(bob.send('Hey Alice!'));AChatRoom is an Observer — it watches for changes to each User's name.
BChatRoom is a Facade — it simplifies a complex subsystem of unrelated classes.
CChatRoom is a Mediator — it centralizes communication so User instances never need direct references to each other; logs "[Alice]: Hi Bob!", "[Bob]: Hey Alice!".
DChatRoom is a Mediator; logs "[Alice]: Hi Bob!", "[Alice]: Hey Alice!".
What mechanism lets DiscountVisitor apply a different discount rule depending on the item's type, without any if (item instanceof Book) checks inside the visitor itself? What is logged?
javascript
class Book {
constructor(price) {
this.price = price;
}
accept(visitor) {
return visitor.visitBook(this);
}
}
class Electronics {
constructor(price) {
this.price = price;
}
accept(visitor) {
return visitor.visitElectronics(this);
}
}
class DiscountVisitor {
visitBook(book) {
return book.price * 0.9;
}
visitElectronics(item) {
return item.price * 0.95;
}
}
const cart = [new Book(20), new Electronics(100)];
const visitor = new DiscountVisitor();
const total = cart.reduce((sum, item) => sum + item.accept(visitor), 0);
console.log(total);A113 — this relies on "double dispatch": each item's own accept method calls the correspondingly-named visitor method (visitBook vs. visitElectronics), so the correct discount logic runs based on the item's actual type without any type-checking inside the visitor.
B113 — this relies on instanceof checks hidden inside reduce.
C108 — this relies on double dispatch.
D120 — no discount is applied, since accept is never actually invoked.
What is logged?
javascript
class NumberExpr {
constructor(value) {
this.value = value;
}
interpret() {
return this.value;
}
}
class AddExpr {
constructor(left, right) {
this.left = left;
this.right = right;
}
interpret() {
return this.left.interpret() + this.right.interpret();
}
}
class MultiplyExpr {
constructor(left, right) {
this.left = left;
this.right = right;
}
interpret() {
return this.left.interpret() * this.right.interpret();
}
}
// Represents: (2 + 3) * 4
const expr = new MultiplyExpr(new AddExpr(new NumberExpr(2), new NumberExpr(3)), new NumberExpr(4));
console.log(expr.interpret());A9
B20 — this is the Composite pattern, since expressions are nested.
CTypeError: this.left.interpret is not a function
D20 — this is the Interpreter pattern: each node in the expression tree implements interpret(), recursively evaluating its children to compute the whole expression's value.
Sign up free to play
Answer all 10 questions (7 more), see explanations for every answer, and track your score.