Software design: Lecture 38 - Sheraz Pervaiz

35 2 0
Software design: Lecture 38 - Sheraz Pervaiz

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Software design - Lecture 38. The main topics covered in this chapter include: proxy design pattern or surrogate design pattern; issues in accessing object services; solution and motivation for proxy; intend of proxy design pattern; proxy pattern defined;...

1 Software Design Lecture : 38 Proxy Design Pattern OR Surrogate Design Pattern   Sample Code fragment //Client class Customer{ public void someMethod(){ //Create the Service Provider Instance FileUtil futilObj=new FileUtil(); //Access the Service futilObj.writeToFile(“Some Data”); }} Implementation of Code  As part of its implementation, the Customer class creates  an  instance  of  the  FileUtil  class  and  directly  accesses  its  services   In other words, for a client object, the way of accessing a  FileUtil  object  is  fairly  straightforward.  From  the  implementation  it  seems to  be  the  most  commonly used  way for a client object to access a service provider object Issues in Accessing Object Services  In contrast, sometimes a client object may not be able to  access a service provider object (also referred to as a target  object) by normal means may be due to:  The location of the target object – Remote Object  Uninstantiated Object till it is required to be rendered  Special  Behavior:  Client  object  need  special  permission  for  access Solution and Motivation for  Proxy  In  such  cases,  instead  of  having  client  objects  to  deal  with  the  special  requirements  for  accessing  the  target  object, the Proxy pattern suggests using a separate object  referred  to  as  a  proxy  to  provide  a  means  for  different  client  objects  to  access  the  target  object  in  a  normal,  straightforward manner Intend of Proxy Design Pattern The  intent  of  this  pattern  is  to  provide  a  placeholder for an object to control references to  it Proxy Pattern Defined “  Proxy  design  pattern  provides  a  surrogate  or  placeholder  for  another  object  to  control  access to it “ Working of Proxy Design Pattern  The  Proxy  object  offers  the  same  interface  as  the  target  object  The  Proxy  object  interacts  with  the  target  object  on  behalf  of  a  client  object  and  takes  care  of  the  specific  details of communicating with the target object 10 Proxy Design Pattern   Client  objects  are  no  longer  needed  to  deal  with  the  special  requirements  for  accessing  the  services  of  the  target object 21 Communication Mechanism i In  general,  a  client  object  cannot  directly  access a remote object by normal means.  ii In  order  to  make  it  possible  for  a client object  to access the services of a remote object as if it  is  a  local  object,  the  RMIC­generated  stub  of  the  remote  object  class  and  the  remote  interface  need  to  be  copied  to  the  client  computer 22 iii.  The stub acts as a (Remote) proxy for the remote object  and  is  responsible  for  forwarding  method  invocations  on  the  remote  object to the server where the actual remote  object  implementation  resides.  Whenever  a  client  references  the  remote  object,  the  reference  is,  in  fact,  made to a local stub. That means, when a client makes a method call on the remote object, it is first  received  by  the  local  stub  instance.  The  stub  forwards  this  call  to  the  remote  server.  On  the  server  the  RMIC  generated skeleton of the remote object receives this call.  As shown in diagram next 23 24 Example of Proxy Design Pattern Consider an image viewer program that lists and  displays high resolution photos. The program has  to  show  a  list  of  all  photos  however  it  does  not  need  to  display  the  actual  photo  until  the  user  selects an image item from a list 25 Class Diagram 26 27 Java Code 28 package proxy;  /** * Subject Interface */ public interface Image  {  public void showImage();  } 29 package proxy;  /** * Proxy */  public class ImageProxy implements Image  {   private String imageFilePath;  /** * Reference to RealSubject */  private Image proxifiedImage;  public ImageProxy(String imageFilePath)  {  this.imageFilePath= imageFilePath; 30 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();  } } 31 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 } } 32 package proxy;  public class ImageViewer  { public static void main(String[] args) {   Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");  Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");  Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");  // assume that the user clicks on Image one item in a list // this  would cause the program to call showImage() for that image  only // note that in this case only image one was loaded into  memory  highResolutionImage1.showImage(); 33 // consider using the high resolution image object directly  Image highResolutionImageNoProxy1 = new  HighResolutionImage("veryHighResPhoto1.jpeg"); Image highResolutionImageNoProxy2 = new HighResolutionImage("veryHighResPhoto2.jpeg");  Image highResolutionImageBoProxy3 = new HighResolutionImage("veryHighResPhoto3.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 // this is a  waste of memory resources } } 34 Comparison of Proxy with other  Facade    Proxy i A Proxy object represents a single object ii   The  client  object  cannot  access  the  target  object  directly iii A  Proxy  object  provides  access  control  to  the  single  target object 35 Facade A Faỗade object represents a subsystem of objects The client  object  does  have  the  ability  to  access  the subsystem objects directly, if needed   A Faỗade object provides a simplified higher levelinterfacetoasubsystemofcomponents

Ngày đăng: 05/07/2022, 14:09

Mục lục

    Proxy Design Pattern OR Surrogate Design Pattern

    Issues in Accessing Object Services

    Solution and Motivation for Proxy

    Intend of Proxy Design Pattern

    Working of Proxy Design Pattern

    Remote Method Invocation -RMI

    Example of Proxy Design Pattern

    Comparison of Proxy with other Facade

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan