From the course: Complete Guide to Java Design Patterns: Creational, Behavioral, and Structural

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Understand the Proxy pattern

Understand the Proxy pattern

In the proxy pattern, a proxy is a class that controls access to the functionality of another class. For example, imagine that you have a class that does some expensive setup when you create a new instance of it. When describing this pattern, this class is sometimes referred to as the subject. So let's say, for example, that the first time the subject is used, it has lots of expensive calls to a database, and then later it does something with the data. This code might be in a third-party library, so you might not have access to it. In this case, you only want the expensive setup to happen once, the first time the object is created. After that, you just want to do the part where it processes the data. You don't really want the classes that call this code to have to implement the logic about only doing this setup once. So in this case, you'd have a proxy object in between, and that controls access to the class with the expensive setup. Then the client just calls the proxy. It doesn't…

Contents