The real-world analogy: modular skyscrapers
Imagine designing a modern skyscraper. You don't lay concrete plumbing and wiring fixed inside every single brick. Instead, you design modular structural patterns:
- Utility Shaft (Chain of Responsibility / Middleware): Electric, water, and fiber-optic cables run down a single centralized shaft. Each floor taps into the shaft, gets its supply, and passes the remaining capacity onward. If one floor blocks water flow, it doesn't shut down the electricity.
- Standard Power Outlet Registry (Strategy & Registry): No matter which device you buy (vacuum, phone charger, toaster), they all conform to a standard two- or three-prong wall outlet configuration. The wall outlet registry manages which plugs are connected, letting the building handle them uniformly.
Why this page exists
"Have you used design patterns?" goes badly when you recite GoF. It goes well when you point at code: "the ingestion layer is Strategy + Registry, the API middleware is Chain of Responsibility, the alert worker is Observer." Here are the real ones.
LandAI — source registry & compliance gate
Each external source is an adapter behind a common interface (Strategy), looked up by a Registry, and fronted by a Guard / Proxy (RobotsGate) that can refuse to run a non-permitted source.
TypeScript
interface SourceAdapter { id: string; permitted: boolean; fetch(q: Query): Promise<Raw>; }
const SOURCE_REGISTRY: Record<string, SourceAdapter> = {
osm_overpass: overpassAdapter, // permitted (ODbL)
listing_portal: portalAdapter, // permitted = false → RobotsGate blocks
};
function ingest(sourceId: string, q: Query) {
const src = SOURCE_REGISTRY[sourceId];
if (!src?.permitted) throw new ComplianceError(sourceId); // the gate
return src.fetch(q);
}
Python
from abc import ABC, abstractmethod
from typing import Dict
class Query: pass
class Raw: pass
class ComplianceError(Exception): pass
class SourceAdapter(ABC):
@property
@abstractmethod
def id(self) -> str: pass
@property
@abstractmethod
def permitted(self) -> bool: pass
@abstractmethod
def fetch(self, q: Query) -> Raw: pass
class OverpassAdapter(SourceAdapter):
id = "osm_overpass"
permitted = True
def fetch(self, q: Query) -> Raw:
return Raw()
class PortalAdapter(SourceAdapter):
id = "listing_portal"
permitted = False
def fetch(self, q: Query) -> Raw:
return Raw()
SOURCE_REGISTRY: Dict[str, SourceAdapter] = {
"osm_overpass": OverpassAdapter(),
"listing_portal": PortalAdapter()
}
def ingest(source_id: str, q: Query) -> Raw:
adapter = SOURCE_REGISTRY.get(source_id)
if not adapter or not adapter.permitted:
raise ComplianceError(source_id)
return adapter.fetch(q)
Java
import java.util.HashMap;
import java.util.Map;
class Query {}
class Raw {}
class ComplianceException extends RuntimeException {
public ComplianceException(String id) { super(id); }
}
public interface SourceAdapter {
String getId();
boolean isPermitted();
Raw fetch(Query q);
}
public class OverpassAdapter implements SourceAdapter {
public String getId() { return "osm_overpass"; }
public boolean isPermitted() { return true; }
public Raw fetch(Query q) { return new Raw(); }
}
public class PortalAdapter implements SourceAdapter {
public String getId() { return "listing_portal"; }
public boolean isPermitted() { return false; }
public Raw fetch(Query q) { return new Raw(); }
}
public class Ingestor {
private static final Map<String, SourceAdapter> registry = new HashMap<>();
static {
registry.put("osm_overpass", new OverpassAdapter());
registry.put("listing_portal", new PortalAdapter());
}
public Raw ingest(String sourceId, Query q) {
SourceAdapter adapter = registry.get(sourceId);
if (adapter == null || !adapter.isPermitted()) {
throw new ComplianceException(sourceId);
}
return adapter.fetch(q);
}
}
C++
#include <string>
#include <unordered_map>
#include <memory>
#include <stdexcept>
class Query {};
class Raw {};
class SourceAdapter {
public:
virtual ~SourceAdapter() = default;
virtual std::string getId() const = 0;
virtual bool isPermitted() const = 0;
virtual Raw fetch(const Query& q) = 0;
};
class OverpassAdapter : public SourceAdapter {
public:
std::string getId() const override { return "osm_overpass"; }
bool isPermitted() const override { return true; }
Raw fetch(const Query& q) override { return Raw(); }
};
class PortalAdapter : public SourceAdapter {
public:
std::string getId() const override { return "listing_portal"; }
bool isPermitted() const override { return false; }
Raw fetch(const Query& q) override { return Raw(); }
};
class Ingestor {
private:
std::unordered_map<std::string, std::shared_ptr<SourceAdapter>> registry;
public:
Ingestor() {
registry["osm_overpass"] = std::make_shared<OverpassAdapter>();
registry["listing_portal"] = std::make_shared<PortalAdapter>();
}
Raw ingest(const std::string& sourceId, const Query& q) {
auto it = registry.find(sourceId);
if (it == registry.end() || !it->second->isPermitted()) {
throw std::runtime_error("ComplianceError: " + sourceId);
}
return it->second->fetch(q);
}
};
- Graceful degradation = Strategy with a fallback chain: FAISS → NumPy, PostGIS → in-memory, XGBoost → scikit-learn. Same interface, swap the implementation by availability.
- Provenance envelope = a Decorator/wrapper that attaches
source · license · confidence · freshnessto every value.
StockVision — middleware & engines
- Chain of Responsibility / Decorator: the request passes through auth → rate-limit → CSP → B2B-gateway middleware, each free to short-circuit. Adding a concern = adding a link, not editing the others.
- Strategy + Composite: the conviction score combines 40+ weighted factors; each factor is a small strategy, the composite aggregates them.
- Observer + Scheduler: the APScheduler alert worker observes price changes and notifies subscribers out of band.
- Policy / Strategy: subscription plan-gating selects behavior by tier, enforced server-side.
StockStump — pricing & trading
- Strategy:
PriceEngineService(performance-based jumps) vsPlayerPriceService(random tick) are interchangeable price-move strategies. - State: a player is
TRADINGorFROZEN(lock active); the trade endpoint's behavior depends on that state object rather than scattered booleans. - Adapter: cricket-API responses are mapped into internal DTOs, isolating the domain from a third-party schema.
For each, say the force and the win: "sources vary and some are illegal → Strategy + a Registry + a Gate → I can add a licensed feed without touching callers and the illegal one can't run." That's a senior-sounding answer.
Interactive Quiz
1.
2.
3.