A constructor that downloads 200 MB
new ArchiveSeries(studyId) does what its name promises. It connects to the hospital's image archive and downloads every slice of one scan, so that slice(n) can hand any of them to the viewer at once. In this archive a typical CT study is about 400 slices of half a megabyte each, 200 MB in all, and the download takes several seconds even on the hospital's own network.
That was fine while the viewer opened one study at a time. Then the radiologists asked for a worklist: the morning's 40 studies in a table, with patient, body part and slice count, so they could pick the next one. The obvious code built an ArchiveSeries for each row, and tried to pull 8 GB into the viewer's memory before showing a table. From that list the radiologist opens one study, maybe two.
The viewer itself was not wrong. It needs something it can ask for a slice, the ImageSeries interface: patientName(), sliceCount(), slice(n). What was wrong was paying for the slices when the object is created instead of when one is asked for, and the fix could not touch the viewer's code, which three other screens share.
Something that answers for the scan
The team wrote LazySeries. It is built from the worklist row, which already carries the patient's name and the slice count, and it holds no slices at all. It implements ImageSeries just as ArchiveSeries does, creates the ArchiveSeries only when it needs one, and passes calls on to it.
Pattern catalogues call an object with that job a Proxy, and the object it stands in for the real subject. The proxy decides whether, when and how each call reaches the real subject, and code that only uses the interface does not change when a proxy takes the real subject's place. One that delays creating an expensive object until the first call that needs it, as LazySeries does, is a virtual proxy.
The viewer holds an ImageSeries. At runtime it is a LazySeries, which builds the ArchiveSeries the first time a slice is asked for.
- The worklist builds 40
LazySeriesobjects from its rows, without contacting the archive. - The viewer calls
patientName()andsliceCount()on each one, and eachLazySeriesanswers from its row. - The radiologist opens one study, and the viewer calls
slice(1)on it. - That
LazySeriesfinds it has no real subject yet, creates theArchiveSeries, and forwards the call. - Later
slice(n)calls on that series still go through theLazySeries, which forwards them to theArchiveSeriesit now holds.
Laziness moves the cost; it does not remove it. The first slice(1) is now the slow call. If the worklist later grows a thumbnail column that calls slice(200) on every row, all 40 series load as before, and nothing looks different in the code, because the proxy cannot see whether a call is for a thumbnail or a reading. If several threads can make that first call, the creation needs a lock or an equivalent, or two of them build a real subject each.
Two more reasons to stand in front
Medical students on rotation may open a study only after it has been anonymised. A protection proxy, GuardedSeries, checks the user's permission before forwarding slice(n) and refuses the call otherwise. How to decide who may see what is taught in Authorization: roles, relationships & policies; the proxy is only where the check sits. It protects only the calls that pass through it, though. If any code can still construct an ArchiveSeries directly, the guard is a suggestion, so the screens get no access to that constructor and receive the series only through one function that returns the guarded version. A check inside the viewer stops mistakes, not a user who controls the viewer's machine; for that, the archive has to refuse the request too.
Radiologists scroll back and forth through the same slices dozens of times. A real subject that fetched one slice per slice(n) call, instead of the whole scan, would download each of them again every time. A caching proxy keeps the slices it has already fetched and answers repeats from memory. That is safe here because this archive never rewrites the slices of a completed study. For data that does change, the proxy inherits every question about staleness and eviction that Caching layers works through.
These stand-ins can be chained, and the order is part of the design. Put a cache shared by all users outside the guard and a slice cached for a radiologist is handed to a student the guard would have refused, because the cache answers before the guard is asked. The guard goes outermost, so every call is checked before anything answers it.
When the real subject is across town
The archive later moved to a data centre across town, and the object the viewer holds became a remote proxy: an object in the viewer's own process with the same interface, which turns slice(n) into a request over the network to the server that really holds the scan. The viewer's code did not change, and that is the danger.
slice(n) used to read memory. Now it can take two seconds, fail halfway because a connection dropped, or time out while the server is still working. Nothing in the interface says so, and code written for the local version has no reason to expect it: a scroll handler that calls slice(n) thirty times a second, a loop with no error handling. A remote call can be made to look like a local one, but not to behave like one. A remote proxy that admits this is easier to use correctly: slice(n) returns a promise or a future, the errors it can raise are part of its contract, and each call has a deadline. That changes ImageSeries itself, and every caller with it, unless the interface was asynchronous from the start. Timeout and Retry cover how long to wait and whether to try again.
Transparency leaks in a quieter way too, local or remote: a proxy is a different object from its real subject, so code that compares references or checks for the concrete class ArchiveSeries gets a different answer once a proxy is in place.
A stand-in, not a second viewer
Decorator has the same outline, an object with the same interface wrapped around another, and a different job. A proxy controls access to its subject: whether, when and how a call gets there. A decorator adds behaviour around the calls it passes on, such as timing them or writing them to a log. A wrapper that changes the interface is an Adapter, which also compares the wrappers side by side, and one simpler front for a whole group of classes is a Facade.
GuardedSeries starts applying the department's default contrast settings to each slice, the same study looks different depending on which wrapper a screen happened to receive, and a radiologist comparing two screens sees two images of one scan. The rule also sits in a class nobody opens when they look for display logic. Keep display rules in the viewer's display code, or in a decorator if they really are an extra layer.Drawing one in the Challenge
The Proxy Pattern Challenge asks for a Subject interface shared by the proxy and the real subject, a real subject that does the work, a proxy that controls access to it, and a client that talks to the proxy. Pick one intent and make it readable on the Grid: a lazy proxy whose edge to the real subject says first slice(), or a guard whose edge says if allowed. Before you submit, check two things. The client has no edge to the real subject. Both the proxy and the real subject connect to the interface, since that shared interface is what lets the client stay unchanged.