CSE460 Lecture 24

CSE460 Lecture 24

Software Analysis and Design
Factory, Adapter, and Chain of Responsibilities
(202010)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez

July 24, 2020
Tweet

Transcript

  1. jgs CSE 460 Software Analysis and Design Lecture 24: Additional

    Design Patterns Dr. Javier Gonzalez-Sanchez javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 2 jgs

    Previously …
  3. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 3 jgs

    Next
  4. jgs Chain of Responsibility Avoid coupling the sender of a

    request to its receiver by giving more than one object a chance to handle the request.
  5. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 5 jgs

    Chain of Responsibility
  6. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 6 jgs

    Chain of Responsibility vs Composite vs Decorator
  7. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 7 jgs

    Chain of Responsibility public class Client { public static void main(String[] args) { // Setup Chain of Responsibility ConcreteHandler2 h1 = new ConcreteHandler2(); ConcreteHandler2 h2 = new ConcreteHandler2(); ConcreteHandler1 h3 = new ConcreteHandler1(); h1.setSuccessor(h2); h2.setSuccessor(h3); // Generate and process request int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 }; for (int i = 0; i < requests.length; i++) { h1.HandleRequest(requests[i]); } } }
  8. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 8 jgs

    Handler abstract class Handler { public abstract void HandleRequest(int request); }
  9. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 9 jgs

    ConcreteHandler1 class ConcreteHandler1 extends Handler { public void HandleRequest(int request) { System.out.println("I am the last in line"); } }
  10. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 10 jgs

    ConcreteHandler2 class ConcreteHandler2 extends Handler { protected Handler successor; public void setSuccessor(Handler successor) { this.successor = successor; } public void HandleRequest(int request) { if (request >= 10 && request < 20) { // code here System.out.println("I am the handling it"); } else if (successor != null) { System.out.println("Passing it..."); successor.HandleRequest(request); } } }
  11. jgs Factory Wrapper of a constructor One entire object to

    be built in a single method call
  12. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 12 jgs

    Next
  13. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 13 jgs

    Abstract Factory
  14. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 14 jgs

    Products public interface Shape { void draw(); } public class RoundedRectangle implements Shape { public void draw() { System.out.println("Inside RoundedRectangle/draw() method."); } } public class RoundedSquare implements Shape { public void draw() { System.out.println("Inside RoundedSquare/draw() method."); } } public class Rectangle implements Shape { public void draw() { System.out.println("Inside Rectangle/draw() method."); } }
  15. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 15 jgs

    Factories public abstract class AbstractFactory { Shape getShape(String shapeType) ; } public class ShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }
  16. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 16 jgs

    Factories public class RoundedShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new RoundedRectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new RoundedSquare(); } return null; } }
  17. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 17 jgs

    Client public class Client { public static void main(String[] args) { AbstractFactory shapeFactory = new ShapeFactory(); //get an object of Shape Rectangle Shape shape1 = shapeFactory.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape1.draw(); //get an object of Shape Square Shape shape2 = shapeFactory.getShape("SQUARE"); //call draw method of Shape Square shape2.draw(); ] //get shape factory AbstractFactory shapeFactory1 = new RoundedShapeFactory(); //get an object of Shape Rectangle Shape shape3 = shapeFactory1.getShape("RECTANGLE"); //call draw method of Shape Rectangle shape3.draw(); //get an object of Shape Square Shape shape4 = shapeFactory1.getShape("SQUARE"); //call draw method of Shape Square shape4.draw(); } }
  18. jgs Adapter Convert the interface of a class into another

    interface clients expect
  19. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 19 jgs

    Next
  20. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 20 jgs

    Adapter
  21. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 21 jgs

    Example RectangleAdapter + draw Rectangle + draw + draw
  22. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 22 jgs

    Client public class Client { public static void main(String[] args) { Shape shape = new RectangleAdapter(new Rectangle(); int x1 = 10, y1 = 20; int x2 = 30, y2 = 60; shape.draw(x1, y1, x2, y2); } }
  23. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 23 jgs

    Adapter class RectangleAdapter implements Shape { private Rectangle adaptee; public RectangleAdapter(Rectangle rectangle) { this.adaptee = rectangle; } public void draw(int x1, int y1, int x2, int y2) { int x = Math.min(x1, x2); int y = Math.min(y1, y2); int width = Math.abs(x2 - x1); int height = Math.abs(y2 - y1); adaptee.draw(x, y, width, height); } }
  24. Javier Gonzalez-Sanchez | CSE460 | Fall 2020 | 24 jgs

    Adaptee class Rectangle { public void draw(int x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point”); } }
  25. jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez javiergs@asu.edu

    Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.