Comparison
Decorator patternvsProxy pattern
Decorator pattern
you wrapped the same interface in retries, then logging, then caching, and the caller still sees one thing.
A wrapper that implements the very same interface as the object it wraps, adding behaviour before or after delegating. Because the interface is unchanged it stacks: three decorators around one object is still that one interface, which is what separates it from adapter and facade. Order matters and is easy to get wrong — caching outside retries caches the eventual success, caching inside retries caches the failure — and that is nearly always the bug.
Full entry →Proxy pattern
the object looks and behaves exactly like the real one, and its whole job is deciding whether the call gets through.
A wrapper with the same interface as its subject whose purpose is to control access to it — checking permission, deferring creation, or standing in for something remote. It shares its shape with the decorator and differs in intent: a decorator adds behaviour the caller wants, a proxy manages access the caller should not have to think about. Proxies typically control the subject's lifetime as well, where decorators are handed one already made.
Full entry →