Observer pattern in PHP - Time & Space Complexity
When using the Observer pattern, we want to know how the time to notify observers changes as we add more observers.
We ask: How does notifying all observers grow with the number of observers?
Analyze the time complexity of the following code snippet.
class Subject {
private array $observers = [];
public function attach(callable $observer): void {
$this->observers[] = $observer;
}
public function notify(): void {
foreach ($this->observers as $observer) {
$observer();
}
}
}
This code stores a list of observers and calls each one when notifying.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through all observers to call them.
- How many times: Once for each observer in the list.
As the number of observers grows, the time to notify grows too, because each observer is called once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to observers |
| 100 | 100 calls to observers |
| 1000 | 1000 calls to observers |
Pattern observation: The number of calls grows directly with the number of observers.
Time Complexity: O(n)
This means the time to notify all observers grows linearly as we add more observers.
[X] Wrong: "Notifying observers is always fast and constant time no matter how many observers there are."
[OK] Correct: Each observer must be called, so more observers mean more calls and more time.
Understanding how notifying observers scales helps you design systems that stay responsive as they grow.
"What if each observer itself notifies other observers? How would that affect the time complexity?"
Practice
Observer pattern in PHP programming?Solution
Step 1: Understand the Observer pattern concept
The Observer pattern is designed to let one object notify others about changes.Step 2: Match the purpose with the options
To allow objects to watch and react to changes in another object correctly describes this notification and reaction mechanism.Final Answer:
To allow objects to watch and react to changes in another object -> Option AQuick Check:
Observer pattern = objects watch and react [OK]
- Confusing Observer with database or input handling
- Thinking it speeds up code execution
- Mixing it with unrelated design patterns
Solution
Step 1: Recall the standard method to add observers
In PHP's Observer pattern, the subject callsattach()to add observers.Step 2: Verify method and object roles
The subject manages observers, so$subject->attach($observer);is correct.Final Answer:
$subject->attach($observer); -> Option CQuick Check:
Subject attaches observer = $subject->attach($observer) [OK]
- Calling attach() on observer instead of subject
- Using non-standard method names like addObserver
- Mixing up subject and observer roles
$subject = new Subject();
$observer = new ConcreteObserver();
$subject->attach($observer);
$subject->setState('Hello');
Assuming
ConcreteObserver prints the new state when notified.Solution
Step 1: Understand the flow of notification
The subject changes state and notifies the observer, which prints the new state.Step 2: Identify the observer's reaction
The observer prints the state 'Hello' when notified.Final Answer:
Hello -> Option AQuick Check:
Observer prints new state = Hello [OK]
- Assuming no output without calling notify explicitly
- Confusing printed message with generic text
- Expecting errors without method definitions
class Subject {
private $observers = [];
public function attach($observer) {
$this->observers[] = $observer;
}
public function notify() {
foreach ($this->observers as $observer) {
$observer->update();
}
}
}
$subject = new Subject();
$subject->notify();
Solution
Step 1: Check observer attachment before notify
The code calls notify() without attaching any observers first.Step 2: Understand effect of no observers
Since no observers are attached, notify() loops over an empty array and does nothing.Final Answer:
No observers attached before notify is called -> Option DQuick Check:
Notify without observers = no action [OK]
- Thinking private array causes error
- Assuming attach() method is missing
- Believing notify() should not call update()
Solution
Step 1: Identify how to support multiple reactions
Different observer classes allow each to react uniquely to the same notification.Step 2: Evaluate options for flexibility and design
Create different observer classes implementing a common interface and attach them to the subject uses a common interface but different classes, which is clean and scalable.Final Answer:
Create different observer classes implementing a common interface and attach them to the subject -> Option BQuick Check:
Multiple observers = different classes with common interface [OK]
- Using one observer with complex if-else logic
- Attaching only one observer limits reactions
- Ignoring design principles by using globals
Ready to prove it?
Pass the test and this pattern moves unknown
