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

CSE564 Lecture 18

CSE564 Lecture 18

Software Design
Additional Patterns II
(202111)

Javier Gonzalez-Sanchez
PRO

September 18, 2020
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 564
    Software Design
    Lecture 18: More Patterns II
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Previously …

    View Slide

  3. jgs
    564 00010010
    The System
    D
    B
    A
    C
    D
    B
    A
    C
    Clustering
    TSP -Nearest Neighbor

    View Slide

  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.

    View Slide

  5. jgs
    564 00010010
    Chain of Responsibility

    View Slide

  6. jgs
    564 00010010
    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

  7. jgs
    564 00010010
    Handler
    abstract class Handler {
    protected Handler successor;
    public void setSuccessor(Handler successor) {
    this.successor = successor;
    }
    public abstract void HandleRequest(int request);
    }

    View Slide

  8. jgs
    564 00010010
    ConcreteHandler
    class ConcreteHandler1 extends Handler {
    public override void HandleRequest(int request) {
    if (request >= 0 && request < 10) {
    // code here…
    } else if (successor != null) {
    successor.HandleRequest(request);
    }
    }
    }
    class ConcreteHandler2 extends Handler {
    public override void HandleRequest(int request) {
    if (request >= 10 && request < 20) {
    // code here
    } else if (successor != null) {
    successor.HandleRequest(request);
    }
    }
    }

    View Slide

  9. jgs
    564 00010010
    Chain of Responsibility vs Composite vs Decorator

    View Slide

  10. jgs
    Strategy
    Define a family of algorithms, encapsulate each one,
    and make them interchangeable

    View Slide

  11. jgs
    564 00010010
    Strategy

    View Slide

  12. jgs
    564 00010010
    Strategy
    Context context;
    // Three contexts following different strategies
    context = new Context(new ConcreteStrategyA());
    context.ContextInterface();
    context = new Context(new ConcreteStrategyB());
    context.ContextInterface();
    context = new Context(new ConcreteStrategyC());
    context.ContextInterface();

    View Slide

  13. jgs
    Composite
    Compose objects into tree structures to represent
    part-whole hierarchies. Composite lets clients treat
    individual objects and compositions of objects
    uniformly

    View Slide

  14. jgs
    564 00010010
    Composite

    View Slide

  15. jgs
    564 00010010
    Composite vs Decorator
    § The difference is more one of purpose than implementation

    View Slide

  16. jgs
    564 00010010
    Create a Tree Structure
    Composite root = new Composite("root");
    root.Add(new Leaf("Leaf A"));
    root.Add(new Leaf("Leaf B"));
    Composite comp = new Composite("Composite X");
    comp.Add(new Leaf("Leaf XA"));
    comp.Add(new Leaf("Leaf XB"));
    root.Add(comp);
    root.Add(new Leaf("Leaf C"));
    // Add and remove a leaf
    Leaf leaf = new Leaf("Leaf D");
    root.Add(leaf);
    root.Remove(leaf);
    // Recursively display tree
    root.Display(1);

    View Slide

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

    View Slide

  18. jgs
    564 00010010
    Adapter

    View Slide

  19. jgs
    564 00010010
    Example
    RectangleAdapter
    + draw
    Rectangle
    + draw
    + draw

    View Slide

  20. jgs
    564 00010010
    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

  21. jgs
    564 00010010
    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

  22. jgs
    564 00010010
    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

  23. jgs
    Bridge
    Decouple an abstraction from its implementation so
    that the two can vary independently.

    View Slide

  24. jgs
    564 00010010
    Bridge

    View Slide

  25. jgs
    564 00010010
    Bridge
    Abstraction ab = new RefinedAbstraction();
    // Set implementation and call
    ab.setImplementor (new ConcreteImplementorA() );
    ab.Operation();
    // Change implemention and call
    ab.setImplementor (new ConcreteImplementorB() );
    ab.Operation();

    View Slide

  26. jgs
    564 00010010
    Strategy vs Bridge

    View Slide

  27. jgs
    564 00010010
    Strategy vs Bridge
    Bridge Pattern makes a distinction
    between an abstraction and an
    implementation in such a way that the
    two can vary independently
    Car - Engine
    changing the behavior
    of an object at run
    time.
    Game: Weapons

    View Slide

  28. jgs
    564 00010010
    Questions

    View Slide

  29. jgs
    CSE 564 Software Design
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE564 at ASU.
    They cannot be distributed or used for another purpose.

    View Slide