Algo-Sim › Design Patterns
Proxy: a caching, lazy price service
Easy · Design Patterns · step through it in Java, Python and C++ on your own input
A proxy implements the same interface as the real object and controls access to it: here it creates the slow service only when first needed and caches its answers.
Problem
A remote catalog answers price(item) slowly (the price is 100 + 10·item). Put a CachingProxy with the same interface in front of it: it creates the remote service on the first cache miss, caches every answer, and invalidate(item) forgets one cached price. Return what each price call returns.
Constraints
- 1 ≤ item ≤ 9
- price is O(1) on a hit; a miss makes exactly one remote call
- Clients use the PriceService interface and can’t tell the proxy from the real service
Edge cases to test
- The first call creates the real object
- Concurrent first calls (create once)
- Access denied by the proxy
Follow-up questions
- What kinds of proxy are there?
- Virtual (lazy creation), protection (access control), remote (a stub for another process) and caching proxies.
- How is it different from a decorator?
- Both wrap the same interface; a proxy controls access to the object, a decorator adds behaviour to it.
Solution
Java
interface PriceService {
int price(int item);
}
class RemoteCatalog implements PriceService { // slow: a network call
public int price(int item) {
return 100 + 10 * item;
}
}
class CachingProxy implements PriceService {
private RemoteCatalog real; // created on first miss
private final Map<Integer, Integer> cache = new HashMap<>();
public int price(int item) {
Integer hit = cache.get(item);
if (hit != null) return hit;
if (real == null) real = new RemoteCatalog();
int p = real.price(item);
cache.put(item, p);
return p;
}
public void invalidate(int item) {
cache.remove(item);
}
}Python
class RemoteCatalog: # slow: a network call
def price(self, item):
return 100 + 10 * item
class CachingProxy: # same price() interface
def __init__(self):
self.real = None # created on first miss
self.cache = {}
def price(self, item):
hit = self.cache.get(item)
if hit is not None:
return hit
if self.real is None:
self.real = RemoteCatalog()
p = self.real.price(item)
self.cache[item] = p
return p
def invalidate(self, item):
self.cache.pop(item, None)C++
#include <bits/stdc++.h>
using namespace std;
struct PriceService {
virtual int price(int) = 0;
virtual ~PriceService() = default;
};
struct RemoteCatalog : PriceService {
int price(int item) override {
return 100 + 10 * item;
}
};
class CachingProxy : public PriceService {
unique_ptr<RemoteCatalog> real;
unordered_map<int, int> cache;
public:
int price(int item) override {
auto hit = cache.find(item);
if (hit != cache.end())
return hit->second;
if (!real)
real = make_unique<RemoteCatalog>();
int p = real->price(item);
cache[item] = p;
return p;
}
void invalidate(int item) {
cache.erase(item);
}
};
More Design Patterns problems
- Strategy: swappable discounts (Easy)
- Observer: publish and subscribe (Easy)
- State: a document workflow (Easy)
- Command: undo and redo (Medium)
- Chain of Responsibility: an ATM (Easy)
- Factory: create without naming classes (Easy)
- Singleton: one shared instance (Easy)
- Builder: a pizza step by step (Easy)
- Decorator: stack toppings at runtime (Easy)
- Adapter: two legacy sensors, one interface (Easy)
- Composite: folder sizes (Easy)