250 likes | 491 Views
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
E N D
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
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
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.
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
Introducing the Dynamic Mapping Pattern • Intent • Use a language's underlying class loading mechanism to eliminate explicit domain specific mappings.
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
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(); }
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
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; }
Learning the Dynamic Mapping Pattern:An Example • Sequence diagram
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");
Learning the Dynamic Mapping Pattern:An Example • A Controller Servlet written using Dynamic Mapping Pattern
Learning the Dynamic Mapping Pattern:An Example • Sequence Diagram of the servlet and JSP
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); } }
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); } }
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
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.
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
Field Notes:Using the Dynamic Mapping Pattern • Generic structure of the Dynamic Mapping pattern
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
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.
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.
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
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