Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object."— Presentation transcript:

1

2 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)

3 Proxy Class Diagram

4 Virtual Proxy

5 An image viewer program Lists high resolution photos Displays photos

6 Image interface package proxy; /** * Subject Interface */ public interface Image { public void showImage(); }

7 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; } @Override 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(); }

8 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 } @Override public void showImage() { // Actual Image rendering logic }

9 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 }

10 Remote Proxy

11 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

12 Gumball Class

13 Gumball Monitor

14 Run the monitor Look at source code.

15 Role of the remote proxy

16 Remote Methods 101

17 How the method call happens Client calls method

18 Client Helper forwards to service helper

19 Service helper calls the real object

20 Real object returns result

21 Service helper forwards result to client helper

22 Client helper returns result to client

23

24 Steps in using Java RMI

25 Additional steps

26 STEP 1 Remote Interface

27 STEP 1 Remote Interface

28 STEP 2 Remote Implementation

29 STEP 2 Remote Implementation

30 STEP 3 Create Stubs & Skeletons

31

32

33

34 Hooking up client and server objects

35

36

37 Back to Gumball machine problem

38 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; }

39 State interface extends Serializable import java.io.*; public interface State extends Serializable { public void insertQuarter(); public void ejectQuarter(); public void turnCrank(); public void dispense(); }

40 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.

41 More of the implementation… Look at source code

42

43 Making the call

44

45 Playing CD Covers

46 Playing CD Cover Proxy

47 ImageProxy process

48 Virtual Proxy Look at source code

49 ImageProxy process

50

51 Using Java API’s Proxy to create a protection proxy > Subject request() > InvocationHandler invoke() RealSubject request() Proxy request() InvocationHandler invoke()

52 Matchmaking example See source code

53 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: don’t call us, we will call you. Depend on abstracts. Do not depend on concrete classes. A class should have only one reason to change.

54 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 couldn’t otherwise because of incompatible interfaces. The Façade Pattern provides a unified interface to a set of interfaces in a subsystem. Façade 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.


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

Similar presentations


Ads by Google