Presentation is loading. Please wait.

Presentation is loading. Please wait.

DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern Author : J. Gresh J. McKim J. McKim H. Sanchez H. Sanchez Excerpt From.

Similar presentations


Presentation on theme: "DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern Author : J. Gresh J. McKim J. McKim H. Sanchez H. Sanchez Excerpt From."— Presentation transcript:

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

2 2 Outline Overview Introducing the Bridge Pattern 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 Mapping Pattern

3 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 4 Outline Overview Introducing the Dynamic Mapping Pattern 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 Mapping Pattern

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

6 6 Outline Overview Introducing the Dynamic Mapping Pattern 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 Mapping Pattern

7 7 Problem of the Strategy Pattern How to create CalcTax 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(); } Learning the Dynamic Mapping Pattern :An Example

8 8 Lengthy conditional statements 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; }

9 9 Language's underlying class loading mechanism Class.forName(fullName).newInstance(); 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; } Learning the Dynamic Mapping Pattern :An Example

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

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

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

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

14 14 Lengthy conditional statements Learning the Dynamic Mapping Pattern :An Example 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 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 16 Outline Overview Introducing the Dynamic Mapping Pattern 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 Mapping Pattern

17 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 18 Outline Overview Introducing the Dynamic Mapping Pattern 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 Mapping Pattern Summary

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

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

21 21 Learning the Dynamic Mapping Pattern: Deriving It Factory Performs the action requested by the client Performs the action requested by the client Determines what action to take based upon the specification provided 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 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 the Interface. May or may not return an instance of some other class. May or may not return an instance of some other class. Does not use domain specific mappings. Does not use domain specific mappings.

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

23 23 Outline Overview Introducing the Dynamic Mapping Pattern 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 Mapping Pattern Summary

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

25 The End


Download ppt "DA-YEH University CSIE Pattern-Oriented Design The Dynamic Mapping Design Pattern Author : J. Gresh J. McKim J. McKim H. Sanchez H. Sanchez Excerpt From."

Similar presentations


Ads by Google