The Gang of Four book says the intent is:
Provide a surrogate or placeholder for another object to control access to it.
The goal is to only do some expensive task... when you need to. The authors give the example of separating initializing a document object which can be expensive from using some metadata about the document, which is inexpensive.
We Clojure and FP in generally, its harder to have an abstraction that does two things the same way a class does two things. In clojure you could build a constructor for a record and have it perform the expensive idealization just to fetch some meta-data. But i would recommend not doing that. If some set of functions depend on an another set of expensive functions, but others dont, then just make this explicit.
* Expensive-function "<---" depedent functions
* inexpsive functions
Expensive operation:
(defn open-file
"opening the file may be expensive!!!"
[f]
(swap! f assoc :opened-file "opened file"))
A Function that depends on some data inside the object existing:
(defn read-file [f]
(:opened-file @f))
A function that doesn't rely on that data being their already
(defn file-size [f]
(swap! f assoc :file-size 5))
Lets create a file to see how this works:
(def file (atom {}))
use the inexpensive function:
(file-size file)
Now the dependent function:
(read-file file)
ops that failed! we need to open it first!
(open-file file)
Now it works:
(read-file file)
The proxy pattern is about saving memory or space, their are lots of ways to do this, all of which depend on the context. In generally we should always be aware of:
For example, it would be wise to cache our open-file function from above to make sure that the client doesn't re-do the expensive task each time they call it. Maybe by checking a hash of the file to see if it has changed and only re-opening it if has. Maybe by lazy evaling the file to only open up until the point we need.
However the proxy pattern seems more concerned with hiding an expensive operation from the client, if this operation truly is a problem, then i suggest not doing that. Instead, just separate out the functions and try to indicate that a operation might be expensive.