Pattern Design Basic Design Pattern 5 — Questions and Answers
Question 1: The Visitor pattern is most useful when:
- The object structure rarely changes but new operations are frequently added (Correct answer)
- New types of objects are added frequently but operations stay constant
- Objects need to be notified of changes in other objects
- A complex object must be built step by step
Correct answer: The object structure rarely changes but new operations are frequently added
Visitor lets you add new operations to an existing object hierarchy without modifying the classes, ideal when the structure is stable but operations evolve.
Question 2: What does 'programming to an interface, not an implementation' mean in the context of design patterns?
- Only use abstract classes, never concrete classes
- Depend on abstractions so implementations can be swapped without breaking clients (Correct answer)
- Define every class as an interface in the codebase
- Avoid using inheritance in favor of interfaces
Correct answer: Depend on abstractions so implementations can be swapped without breaking clients
Depending on abstractions (interfaces or abstract classes) rather than concrete types makes code more flexible, extensible, and testable — a core GoF principle.
Question 3: Which pattern is used to interpret sentences in a language by representing its grammar as a class hierarchy?
- Visitor
- Interpreter (Correct answer)
- Command
- Template Method
Correct answer: Interpreter
The Interpreter pattern defines a grammar for a language and provides an interpreter to process sentences, with each grammar rule represented as a class.
Question 4: The Bridge pattern separates abstraction from implementation in order to:
- Reduce the number of classes required by avoiding a Cartesian product explosion (Correct answer)
- Provide a simplified interface to a complex subsystem
- Allow objects to change behavior based on internal state
- Enable lazy loading of expensive resources
Correct answer: Reduce the number of classes required by avoiding a Cartesian product explosion
Bridge prevents a proliferation of subclasses by letting abstraction and implementation vary independently rather than combining every permutation in a single class hierarchy.
Question 5: Favor composition over inheritance is a design principle. Which pattern most directly embodies this principle?
- Template Method
- Factory Method
- Strategy (Correct answer)
- Interpreter
Correct answer: Strategy
Strategy composes an algorithm object into a context rather than inheriting behavior, making it a canonical example of preferring composition over inheritance.
Question 6: When using the Prototype pattern, how is a new object created?
- By calling a factory method with configuration parameters
- By cloning an existing prototype instance (Correct answer)
- By assembling parts through a builder object
- By constructing it via an abstract factory
Correct answer: By cloning an existing prototype instance
The Prototype pattern creates new objects by copying (cloning) a prototypical instance, avoiding the cost of creating objects from scratch.
Question 7: Which of the following is NOT a GoF (Gang of Four) design pattern category?
- Creational
- Structural
- Behavioral
- Architectural (Correct answer)
Correct answer: Architectural
The GoF book defines three pattern categories — Creational, Structural, and Behavioral; Architectural is a separate classification used for higher-level patterns like MVC or microservices.
The Visitor pattern is most useful when: