Proxy pattern 00:00:00 Make a prediction You will distinguish controlling access to a service from changing its public interface and test the first-call boundary. A gallery lists a hundred images but the visitor opens only one. How many full-resolution images must a lazy image proxy load to display that one selection? One, assuming the chosen image is not cached already. The proxy can list metadata cheaply and load actual pixels only when display is requested, while preserving the image interface. 00:00:32 The page at the palace gate A palace page receives requests at the gate. The page can remember an answer, refuse an unauthorized visit or relay the request to the monarch. A visitor still asks the same question at the same gate. A proxy controls access, timing or location while presenting the service contract. If it changes the meaning of a successful request, clients need to know that contract has changed. Proxy is a structural design pattern. A proxy implements the interface of another object, the real subject, and stands before it. Clients talk to the proxy and can not tell by the interface. Rules about access, such as load once, only administrators, at most one call a second, are the same for every client. Written into clients they are copied and forgotten. 00:01:22 Load the large image on demand A gallery displays many filenames, but only the selected picture needs its full-resolution pixels. Loading them all at startup is expensive. Give the gallery an image proxy that implements the same display operation. On the first display it loads the real image and delegates. Display the same image again and verify the cached real object is reused. Specify what another caller should see if the first load fails. Scroll past ten thumbnails without opening any. There should be no original-image loads. Open the same image twice and measure how often its bytes are fetched. The first miss and later hit can share an interface, but a failed fetch still needs an explicit retry policy. 00:02:10 Control access without changing the service contract A report service can be wrapped in a proxy that checks whether the caller may read the requested report before delegation. An authorized caller gets the ordinary report and an unauthorized caller gets an explicit refusal. That access decision is different from translating to an incompatible service interface. Send an unauthorized change through the proxy and count calls on the real service. The expected count is zero. An authorized call reaches it once. If clients can obtain the underlying service directly, the public protection promise cannot be demonstrated by testing the wrapper alone. Two concurrent first displays can both trigger a full load unless initialization is coordinated. The proxy pattern alone does not guarantee one fetch. A decorator adds optional behaviour; an adapter translates an incompatible interface. Name the problem before picking the wrapper label. Hibernate. Lazy relations are represented by proxies that are initialized at first use. Using one after the session is closed raises LazyInitializationException. Spring AOP. Uses JDK dynamic proxies or CGLIB subclasses. The reference explains that self-invocation does not run the advice, because the call does not go through the proxy. 00:03:34 Try an interview follow-up An auditor asks you to prove that nobody reaches accounts without a check. What do you show? The real class is private to a module with one factory. A test lists every interface method and fails if one has no rule. Other services have no database access, only the API. Refusals and permissions carry rule and policy version in an append-only log. A dry run compares new rules with yesterday's traffic. And I name what is left: administrators of the database, covered by separate controls. A gallery displays thumbnails immediately and loads the original when opened. Let a proxy implement the image interface and defer loading. Open the same photo twice, then simulate a failed first load. Specify whether the next open retries and what the caller sees while loading. 00:04:30 What to take with you Same interface, controlled access. Virtual creates late, protection checks, caching remembers, remote travels. One job per proxy, and the real subject stays clean. Answer cheap questions without the real object, create it once, deny by default, guard every method, and give nobody the real one.