Design Patterns Tutorial
Design Patterns Resources
Selected Reading
- Transfer Object Pattern
- Service Locator Pattern
- Intercepting Filter Pattern
- Front Controller Pattern
- Data Access Object Pattern
- Composite Entity Pattern
- Business Delegate Pattern
- Design Patterns - MVC Pattern
- Design Patterns - Visitor Pattern
- Design Patterns - Template Pattern
- Design Patterns - Strategy Pattern
- Design Patterns - Null Object Pattern
- Design Patterns - State Pattern
- Design Patterns - Observer Pattern
- Design Patterns - Memento Pattern
- Design Patterns - Mediator Pattern
- Design Patterns - Iterator Pattern
- Design Patterns - Interpreter Pattern
- Design Patterns - Command Pattern
- Chain of Responsibility Pattern
- Design Patterns - Proxy Pattern
- Design Patterns - Flyweight Pattern
- Design Patterns - Facade Pattern
- Design Patterns - Decorator Pattern
- Design Patterns - Composite Pattern
- Design Patterns - Filter Pattern
- Design Patterns - Bridge Pattern
- Design Patterns - Adapter Pattern
- Design Patterns - Prototype Pattern
- Design Patterns - Builder Pattern
- Design Patterns - Singleton Pattern
- Abstract Factory Pattern
- Design Patterns - Factory Pattern
- Design Patterns - Overview
- Design Patterns - Home
Design Patterns Resources
- Design Patterns - Discussion
- Design Patterns - Useful Resources
- Design Patterns - Quick Guide
- Design Patterns - Questions/Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Design Patterns - Proxy Pattern
Design Patterns - Proxy Pattern
In proxy pattern, a class represents functionapty of another class. This type of design pattern comes under structural pattern.
In proxy pattern, we create object having original object to interface its functionapty to outer world.
Implementation
We are going to create an Image interface and concrete classes implementing the Image interface. ProxyImage is a a proxy class to reduce memory footprint of RealImage object loading.
ProxyPatternDemo, our demo class, will use ProxyImage to get an Image object to load and display as it needs.
Step 1
Create an interface.
Image.java
pubpc interface Image { void display(); }
Step 2
Create concrete classes implementing the same interface.
RealImage.java
pubpc class RealImage implements Image { private String fileName; pubpc RealImage(String fileName){ this.fileName = fileName; loadFromDisk(fileName); } @Override pubpc void display() { System.out.println("Displaying " + fileName); } private void loadFromDisk(String fileName){ System.out.println("Loading " + fileName); } }
ProxyImage.java
pubpc class ProxyImage implements Image{ private RealImage realImage; private String fileName; pubpc ProxyImage(String fileName){ this.fileName = fileName; } @Override pubpc void display() { if(realImage == null){ realImage = new RealImage(fileName); } realImage.display(); } }
Step 3
Use the ProxyImage to get object of RealImage class when required.
ProxyPatternDemo.java
pubpc class ProxyPatternDemo { pubpc static void main(String[] args) { Image image = new ProxyImage("test_10mb.jpg"); //image will be loaded from disk image.display(); System.out.println(""); //image will not be loaded from disk image.display(); } }
Step 4
Verify the output.
Loading test_10mb.jpg Displaying test_10mb.jpg Displaying test_10mb.jpgAdvertisements