1 / 25

DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern

DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern. Author : J. Gresh J. McKim H. Sanchez Excerpt From : PLoP 2005 conference reader: E9460001 Shine Liang. Outline. Overview Introducing the Bridge Pattern

cyrah
Download Presentation

DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DA-YEH University CSIE Pattern-Oriented DesignThe Dynamic Mapping Design Pattern Author:J. Gresh J. McKim H. Sanchez Excerpt From :PLoP 2005 conference reader: E9460001 Shine Liang

  2. Outline • Overview • Introducing the Bridge Pattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic MappingPattern: Deriving It • Field Notes: Using the Dynamic MappingPattern

  3. Overview • A design pattern for the elimination of lengthy conditional statements named Dynamic Mapping. • uses a collection of objects that implement a common interface and dynamic class loading to replace lengthy domain specific conditional logic.

  4. Outline • Overview • Introducing the Dynamic Mapping Pattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic MappingPattern: Deriving It • Field Notes: Using the Dynamic MappingPattern

  5. Introducing the Dynamic Mapping Pattern • Intent • Use a language's underlying class loading mechanism to eliminate explicit domain specific mappings.

  6. Outline • Overview • Introducing the Dynamic MappingPattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic MappingPattern: Deriving It • Field Notes: Using the Dynamic MappingPattern

  7. Learning the Dynamic Mapping Pattern:An Example • Problem of the Strategy Pattern • How to create CalcTax CalcTax* calc_tax = null; If (buyer_country == US) calc_tax = new USTax(); else if (buyer_country == CANADA) calc_tax = new CANTax(); else if (…) calc_tax = new … endif If (calc_tax != null) { float tax_amount = calc_tax >calc(); }

  8. Learning the Dynamic Mapping Pattern:An Example public Animal getAnimal(String name) { if ("Rat".equals(name)) return new Rat(); else if ("Ox".equals(name)) return new Ox(); else if ("Tiger".equals(name)) return new Tiger(); else if ("Rabbit".equals(name)) return new Rabbit(); else if ("Dragon".equals(name)) return new Dragon(); else if ("Snake".equals(name)) return new Snake(); else if ("Horse".equals(name)) return new Horse(); else if ("Ram".equals(name)) return new Ram(); else if ("Monkey".equals(name)) return new Monkey(); else if ("Rooster".equals(name)) return new Rooster(); else if ("Dog".equals(name)) return new Dog(); else if ("Pig".equals(name)) return new Pig(); return null; } • Lengthy conditional statements

  9. Learning the Dynamic Mapping Pattern:An Example • Language's underlying class loading mechanism • Class.forName(fullName).newInstance(); public static final String ANIMAL_PACKAGE_NAME = "POD.MappingFactory.animal"; public Animal getAnimal(String name) { String fullName = ANIMAL_PACKAGE_NAME + "." + name; try { return (Animal) Class.forName(fullName).newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return null; }

  10. Learning the Dynamic Mapping Pattern:An Example • Sequence diagram

  11. Learning the Dynamic Mapping Pattern:An Example • demonstrate • JAVA • Eclipse3 • POD.MappingFactory.animal.client.java; • C++ ”Dynamic Class Loading for C++ on Linux ”by James Norton void *hndl = dlopen("libnewshapes.obj", RTLD_NOW); if(hndl == NULL) { cerr << dlerror() << endl; exit(-1); } void *mkr = dlsym(hndl, "maker");

  12. Learning the Dynamic Mapping Pattern:An Example • A Controller Servlet written using Dynamic Mapping Pattern

  13. Learning the Dynamic Mapping Pattern:An Example • Sequence Diagram of the servlet and JSP

  14. Learning the Dynamic Mapping Pattern:An Example • Lengthy conditional statements public class ControllerTradition { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String operation = request.getParameter("operation"); if (operation == null) { operation = "unknown"; } String address = ""; if (operation.equals("Rat")) { address = "/WEB-INF/Rat.jsp"; } else if (operation.equals("Ox")) { address = "/WEB-INF/Ox.jsp"; } else if (operation.equals("Tiger")) { address = "/WEB-INF/Tiger.jsp"; } else if (operation.equals("Rabbit")) { RequestDispatcher dispatcher = request.getRequestDispatcher(address); dispatcher.forward(request, response); } }

  15. Learning the Dynamic Mapping Pattern:An Example public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { processRequest(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { processRequest(request, response); } protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ActionInterface action = null; String actionName = mPrefix + request.getParameter(mActionName) + mPostFix; try { action = (ActionInterface) Class.forName(actionName).newInstance(); action.processRequest(request, response); RequestDispatcher disp = request.getRequestDispatcher(action.getNextUrl()); disp.forward(request, response); } catch (Throwable t) { throw new ServletException(t); } }

  16. Outline • Overview • Introducing the Dynamic MappingPattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic MappingPattern: Deriving It • Field Notes: Using the Dynamic MappingPattern

  17. An Observation About Using Design Patterns • Use of the Dynamic Mapping design pattern eliminates any representation of a lengthy conditional created and maintained by the authors of a system. • The Dynamic Mapping design pattern defers this responsibility to the underlying language by using dynamic class loading and thereby reduces the number of potential errors that can be generated, reduces the amount that the system will grow, and adds additional opportunities for reuse.

  18. Outline • Overview • Introducing the Dynamic MappingPattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic Mapping Pattern: Deriving It • Field Notes: Using the Dynamic MappingPattern • Summary

  19. Field Notes:Using the Dynamic Mapping Pattern • Generic structure of the Dynamic Mapping pattern

  20. Learning the Dynamic Mapping Pattern: Deriving It • Interface • Defines the interface of the objects created by the Factory • Defines methods that can be executed by either the Factory or the Client

  21. Learning the Dynamic Mapping Pattern: Deriving It • Factory • Performs the action requested by the client • Determines what action to take based upon the specification provided by the client • Can execute one or more methods of the Interface • May or may not return an instance of the Interface. • May or may not return an instance of some other class. • Does not use domain specific mappings.

  22. Learning the Dynamic Mapping Pattern: Deriving It • Implementation • Concrete definition of the Interface. • Client • Makes requests to the Factory for a specific action. • Known Uses • Can be used in association with Factory patterns and Controller patterns.

  23. Outline • Overview • Introducing the Dynamic MappingPattern • Learning the Dynamic Mapping Pattern: An Example • An Observation About Using Design Patterns • Learning the Dynamic MappingPattern: Deriving It • Field Notes: Using the Dynamic Mapping Pattern • Summary

  24. Field Notes:Using the Dynamic Mapping Pattern • Dynamic mapping provides the dvantages • Reduction of errors • The potential for errors that can occur through the use of domain specific mappings is reduced • Constant Size • Domain specific mappings will grow linearly with the number of mappings that are required • Reuse • Use of the Dynamic Mapping design pattern produces classes that are highly reusable

  25. The End

More Related