Function design_pattern_examples_rust::proxy::proxy_exercise
source · pub fn proxy_exercise() -> Result<(), String>
Expand description
Example of using the “Proxy” design pattern.
The Proxy pattern is used when a large or expensive object cannot be represented directly in the program, typically because the object is in another process or even another system altogether.
In this exercise, a Proxy struct implements the same trait as the Real struct, making the Proxy struct look like the Real struct. Calls made on the Proxy struct are passed to the Real struct where the work is actually done (in this case, a munged string with the text “Real struct received ‘xxxx’”.
The only difference in output here is one additional line for the first call showing the real struct being instantiated. The subsequent calls into the Proxy struct do not show this line.