Proxy design pattern provides the control for accessing the original object. It adds a way to hide the complexities of the real object.
Four use cases:
Virtual proxyused when object creation expensive, real object is only created when a client accesses the object.Remote proxyused for local representative for an object that resides in a different address space.Protective proxyused to control access to a sensitive real object.Smart proxyused for some additional actions when an object is accessed. Like loading DB object into memory.
A real life scenario is college internet, which restricts few site access. The proxy first checks the host user are
connecting to, if it is not part of restricted site list, then it connects to the real internet.
This is related to protection proxy.
ProxyInternetis the proxy object ofRealInternetobject.- Client only access proxy internet's
connectmethod. It checks site names and if allowed, then it call real object's method.
Exception exception = Assertions.assertThrows(IllegalAccessException.class, () -> {
internet.connect("www.bannedsite.com");
});
Assertions.assertEquals("You don't have permission to access this site", exception.getMessage());
// or
try {
internet.connect("www.abc.com");
} catch (Exception e) {
e.printStackTrace();
}
Assertions.assertEquals("Connecting to internet", outputStreamCaptor.toString().trim());