$30 off During Our Annual Pro Sale. View Details »

CSE460 Lecture 19

CSE460 Lecture 19

Software Analysis and Design
Connecting the Dots
(202103)

Javier Gonzalez-Sanchez
PRO

July 19, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 460
    Software Analysis and Design
    Lecture 19: Connecting the Dots
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    460 0010011
    Announcement
    § Assignment 05 (Patterns) is due March 29. It opens at the end of the
    lecture

    View Slide

  3. jgs
    460 0010011
    Next

    View Slide

  4. jgs
    460 0010011
    Factory

    View Slide

  5. jgs
    460 0010011
    Using a Factory
    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();
    }
    }

    View Slide

  6. jgs
    460 0010011
    Chain of Responsibility

    View Slide

  7. jgs
    460 0010011
    Using Chain of Responsibility
    // Setup Chain of Responsibility
    Handler h1 = new ConcreteHandler1();
    Handler h2 = new ConcreteHandler2();
    Handler h3 = new ConcreteHandler3();
    h1.SetSuccessor(h2);
    h2.SetSuccessor(h3);
    // Generate and process request
    int[] requests = { 2, 5, 14, 22, 18, 3, 27, 20 };
    foreach (int i=0; ih1.HandleRequest(request[i]);
    }

    View Slide

  8. jgs
    460 0010011
    Decorator

    View Slide

  9. jgs
    460 0010011
    Using a Decorator
    class MainApp {
    static void main() {
    // Create ConcreteComponent and two Decorators
    ConcreteComponent c = new ConcreteComponent();
    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
    ConcreteDecoratorB d2 = new ConcreteDecoratorB();
    // Link decorators
    d1.SetComponent(c);
    d2.SetComponent(d1);
    d2.Operation();
    }
    }

    View Slide

  10. jgs
    Adapter
    Convert the interface of a class into another interface
    clients expect

    View Slide

  11. jgs
    460 0010011
    Adapter

    View Slide

  12. jgs
    460 0010011
    Example
    RectangleAdapter
    + draw
    Rectangle
    + draw
    + draw

    View Slide

  13. jgs
    460 0010011
    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);
    }
    }

    View Slide

  14. jgs
    460 0010011
    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);
    }
    }

    View Slide

  15. jgs
    460 0010011
    Adaptee
    class Rectangle {
    public void draw(int x, int y, int width, int height) {
    System.out.println("Rectangle with coordinate left-down point”);
    }
    }

    View Slide

  16. jgs
    Assignment 04
    Connecting the Dots

    View Slide

  17. jgs
    460 0010011
    Team
    1 2 3

    View Slide

  18. jgs
    460 0010011
    Idea
    Main
    Factory
    Gift Ball Box Envelop

    View Slide

  19. jgs
    460 0010011
    Idea
    wrapper2
    Main wrapper1
    Factory
    decoration
    Envelope
    Box
    Gift
    wrapper3
    ball
    Supervisor
    Is big > 10, box Is small < 5, envelop

    View Slide

  20. jgs
    460 0010011

    View Slide

  21. jgs
    460 0010011
    Main

    View Slide

  22. jgs
    460 0010011
    Factory

    View Slide

  23. jgs
    460 0010011
    Chain of Responsibilities

    View Slide

  24. jgs
    460 0010011
    Decorator

    View Slide

  25. jgs
    460 0010011
    Decorator

    View Slide

  26. jgs
    460 0010011
    Observer

    View Slide

  27. jgs
    460 0010011

    View Slide

  28. jgs
    CSE 460 Software Analysis and Design
    Javier Gonzalez-Sanchez
    [email protected]
    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.

    View Slide