54
The Proxy Pattern

Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object

Embed Size (px)

DESCRIPTION

Proxy Class Diagram

Citation preview

Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object. The another object remote (remote proxy) expensive to create (virtual proxy) in need of securing (protection proxy) Proxy Class Diagram Virtual Proxy An image viewer program Lists high resolution photos Displays photos Image interface package proxy; /** * Subject Interface */ public interface Image { public void showImage(); } Proxy implementation package proxy; /** * Proxy */ public class ImageProxy implements Image { /** * Private Proxy data */ private String imageFilePath; /** * Reference to RealSubject */ private Image proxifiedImage; public ImageProxy(String imageFilePath) { this.imageFilePath= imageFilePath; public void showImage() { // create the Image Object only when the image is required to be shown proxifiedImage = new HighResolutionImage(imageFilePath); // now call showImage on realSubject proxifiedImage.showImage(); } RealSubject Implementation package proxy; /** * RealSubject */ public class HighResolutionImage implements Image { public HighResolutionImage(String imageFilePath) { loadImage(imageFilePath); } private void loadImage(String imageFilePath) { // load Image from disk into memory // this is heavy and costly operation public void showImage() { // Actual Image rendering logic } An image viewer program package proxy; public class ImageViewer { public static void main(String[] args) { // assuming that the user selects a folder that has 3 images Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg"); Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg"); highResolutionImage1.showImage(); // consider using the high resolution image object directly Image highResolutionImageNoProxy1 = new HighResolutionImage("sample/veryHighResPhoto1.jpeg"); Image highResolutionImageNoProxy2 = new HighResolutionImage("sample/veryHighResPhoto2.jpeg"); // assume that the user selects image two item from images list highResolutionImageNoProxy2.showImage(); // note that in this case all images have been loaded into memory // and not all have been actually displayed and this is a waste of memory resources } Remote Proxy Revisit the Gumball machine example The same example covered in the State pattern Now we want to add some monitor a collection of Gumball machines Gumball Class Gumball Monitor Run the monitor Look at source code. Role of the remote proxy Remote Methods 101 How the method call happens Client calls method Client Helper forwards to service helper Service helper calls the real object Real object returns result Service helper forwards result to client helper Client helper returns result to client Steps in using Java RMI Additional steps STEP 1 Remote Interface STEP 1 Remote Interface STEP 2 Remote Implementation STEP 2 Remote Implementation STEP 3 Create Stubs & Skeletons Hooking up client and server objects Back to Gumball machine problem Gumball Machine remote interface import java.rmi.*; public interface GumballMachineRemote extends Remote { public int getCount() throws RemoteException; public String getLocation() throws RemoteException; public State getState() throws RemoteException; } State interface extends Serializable import java.io.*; public interface State extends Serializable { public void insertQuarter(); public void ejectQuarter(); public void turnCrank(); public void dispense(); } Use of keyword transient public class NoQuarterState implements State { transient GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("You inserted a quarter"); gumballMachine.setState(gumballMachine.getHasQuarterState()); } // other methods } The use of transient to to ensure that the seriolzation does not involve this object as well. More of the implementation Look at source code Making the call Playing CD Covers Playing CD Cover Proxy ImageProxy process Virtual Proxy Look at source code ImageProxy process Using Java APIs Proxy to create a protection proxy > Subject request() > InvocationHandler invoke() RealSubject request() Proxy request() InvocationHandler invoke() Matchmaking example See source code Summary so far.. OO Basics Abstraction Encapsulation Inheritance Polymorphism OO Principles Encapsulate what varies Favor composition over inheritance Program to interfaces not to implementations Strive for loosely coupled designs between objects that interact Classes should be open for extension but closed for modification. Depend on abstracts. Do not depend on concrete classes. Only talk to your friends Hollywood principles: dont call us, we will call you. Depend on abstracts. Do not depend on concrete classes. A class should have only one reason to change. Summary so far OO Patterns Strategy Pattern defines a family of algorithms, Encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically. Decorator Pattern attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative for sub-classing for extending functionality Singleton Pattern ensure a class only has one instance, and provide a global point of access to it The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldnt otherwise because of incompatible interfaces. The Faade Pattern provides a unified interface to a set of interfaces in a subsystem. Faade defines a higher level interface that makes the subsystem easier to use. The Template Pattern defines steps of an algorithm. Subclasses cannot change the algorithm (final). It facilitates code reuse. The Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory method lets a class defer instantiation to the subclasses. The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class. The Proxy Pattern provides a surrogate or placeholder for another object to control access to it.