Pattern Design Architectural and Enterprise Patterns 1 — Questions and Answers
Question 1: The Model-View-Controller (MVC) architectural pattern separates concerns into:
- Data model, user interface, and request handling logic (Correct answer)
- Service layer, data access layer, and presentation layer
- Domain model, repository, and service
- Command handler, query handler, and event bus
Correct answer: Data model, user interface, and request handling logic
MVC divides an application into Model (data/business logic), View (UI), and Controller (handles user input and updates model/view).
Question 2: The Repository pattern abstracts:
- Data access logic from the domain model (Correct answer)
- UI rendering from business logic
- Service calls from controllers
- Event publishing from event handling
Correct answer: Data access logic from the domain model
Repository acts as an in-memory collection of domain objects while hiding the details of data access, making it easy to swap storage implementations.
Question 3: The Service Locator pattern is often considered an anti-pattern because it:
- Hides dependencies and makes testing difficult (Correct answer)
- Tightly couples the service to its consumer
- Prevents dependency injection
- Creates circular dependencies by default
Correct answer: Hides dependencies and makes testing difficult
Service Locator hides a class's dependencies since they are resolved at runtime from a global registry rather than being declared explicitly.
Question 4: CQRS stands for Command Query Responsibility Segregation and separates:
- Write operations (commands) from read operations (queries) (Correct answer)
- Domain logic from infrastructure logic
- Synchronous calls from asynchronous calls
- HTTP requests from background jobs
Correct answer: Write operations (commands) from read operations (queries)
CQRS uses separate models for modifying state (commands) and reading state (queries), allowing each side to be optimized independently.
Question 5: The Event Sourcing pattern stores application state as:
- A sequence of immutable events rather than the current state snapshot (Correct answer)
- A full snapshot of the database at each transaction
- JSON documents representing each entity
- A log of SQL UPDATE statements
Correct answer: A sequence of immutable events rather than the current state snapshot
Event Sourcing persists every state change as an event, allowing the current state to be reconstructed by replaying the event history.
Question 6: Which architectural pattern is used when multiple services must participate in a distributed transaction without two-phase commit?
- Saga (Correct answer)
- Repository
- CQRS
- Mediator
Correct answer: Saga
The Saga pattern coordinates distributed transactions as a sequence of local transactions with compensating actions to handle failures.
The Model-View-Controller (MVC) architectural pattern separates concerns into: