From the course: Java Design Patterns: Structural

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: The Proxy pattern

Solution: The Proxy pattern - Java Tutorial

From the course: Java Design Patterns: Structural

Start my 1-month free trial

Solution: The Proxy pattern

(upbeat electronic music) - [Instructor] Let's have a look at my solution to the exercise. Yours might look a bit different to mine, and don't worry if it does, that's fine. So I've created a class called SuperstoreInventoryProxy, and this class implements the inventory interface. It overrides the getInventory method. And in here, I check if the inventory has already been created, and if it hasn't, then I create a new one. Then I return that inventory. The only other change is in the main class. Instead of creating a SuperstoreInventory object, I'm creating a SuperstoreInventoryProxy object. This means that I'm not creating the long list of items in the inventory until I call the PrintInventory method later on. This is how the proxy pattern can help you save memory and improve startup times in your application.

Contents