Case study demo
Object-oriented design patterns
Classic GoF patterns with small examples (low-level design, not distributed systems).
An interactive demo of how this system behaves. It runs in your browser.
Pick a pattern, then press Next step to follow one call through its objects. The words under the picture explain what you saw.
Factory Method
With this case: The shared code is the same in both cases. Here the subclass chose RedisCache.
- Your code — picks a service — healthy
- CacheService — the shared steps — healthy — NOW
- RedisCacheService — writes createCache() — healthy
- RedisCache — the object made — healthy
- A call
- A new object made
- Load or read
Step 1 of 4 · CacheService
Done: Your code makes a RedisCacheService
- What happens
- This subclass has no constructor of its own. So the one written in the base class runs.
- Why
- The steps every cache service shares are written once, in the base class.
- Next
- The constructor asks for a cache
Event log
3 more to go. Press a line to go back to that step.
Step 1 of 4: CacheService. Your code makes a RedisCacheService. This subclass has no constructor of its own. So the one written in the base class runs.
What it is
A base class leaves one step, a method that creates an object, for each subclass to write.
The problem it solves
CacheService has the same steps for every kind of cache, but each deployment needs a different cache object. Without the pattern, the base class would need an if for every kind.
When to use it
- The shared steps stay the same, and only the object they work with changes.
- You want to add a new kind by adding a subclass, without editing the base class.
What it costs
Each new kind needs a new subclass. When there are only two kinds and they rarely change, one function with an if is shorter. That simpler version is called a simple factory.
class CacheService {
constructor() { this.cache = this.createCache() }
createCache() { throw new Error('a subclass must choose') }
get(key) { return this.cache.get(key) }
}
class RedisCacheService extends CacheService {
createCache() { return new RedisCache() }
}
class LocalCacheService extends CacheService {
createCache() { return new InMemoryCache() }
}What these pictures simplify. The parts in these pictures are objects and functions inside one program, not servers. A call between them takes no network trip. The numbers, such as a pool of 10 connections and an 8 MB photo, are examples.
Design walkthrough: Object-oriented design patterns
Design patterns are named answers to problems that keep coming back in code. They belong to low-level design: the classes inside one service, not servers and networks.
How to choose
ChoosingFrom a problem to a pattern
Many if-else branches to Strategy
A checkout picks card, wallet or bank transfer with a long if-else. Put each way to pay in its own class with the same method, and choose one at run time.
One event, many listeners to Observer
A price changes, and the app, an email alert and a dashboard must all react. Each one subscribes, and the price object notifies every subscriber.
An interface that does not fit to Adapter
A new SMS vendor uses different method names from your code. Wrap it in a class that offers the methods your code already calls.
Creation logic in many places to Factory method
Many places build notification objects with "new". Move that choice into one method, so adding a new kind changes one place.
A costly or guarded object to Proxy
A large image is slow to load. A stand-in object with the same interface loads the real one only when it is first used.
Trade-offs
- Add a pattern now, or wait
- A good default
- Wait until the second or third case shows the real shape of the problem.
- What it costs
- You change the code once more, later.
- A Singleton, or pass the object in
- A good default
- Pass it in. A test can then swap it for a fake.
- What it costs
- Constructors take one more argument.
Common mistakes
- Starting from a pattern and looking for a place to use it. Start from the problem.
- Using a Singleton as a global variable. Hidden shared state makes code hard to test.
Questions an interviewer might ask
How do Strategy and State differ?
The classes look the same. With Strategy the caller picks the behaviour. With State the object changes its own behaviour as it moves between states.
Where do you see Observer in a distributed system?
In publish and subscribe. A message broker plays the subject, and services are the observers.
How do Proxy and Adapter differ?
A Proxy keeps the same interface and adds control, such as lazy loading or an access check. An Adapter changes the interface.