$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 javiergs@asu.edu javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment
  2. jgs Previously …

  3. jgs 564 00010010 The System D B A C D

    B A C Clustering TSP -Nearest Neighbor
  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. jgs 564 00010010 Chain of Responsibility

  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; i<requests.lenght; i++) { h1.HandleRequest(request[i]); }
  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); }
  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); } } }
  9. jgs 564 00010010 Chain of Responsibility vs Composite vs Decorator

  10. jgs Strategy Define a family of algorithms, encapsulate each one,

    and make them interchangeable
  11. jgs 564 00010010 Strategy

  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();
  13. jgs Composite Compose objects into tree structures to represent part-whole

    hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
  14. jgs 564 00010010 Composite

  15. jgs 564 00010010 Composite vs Decorator § The difference is

    more one of purpose than implementation
  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);
  17. jgs Adapter Convert the interface of a class into another

    interface clients expect
  18. jgs 564 00010010 Adapter

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

    + draw
  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); } }
  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); } }
  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”); } }
  23. jgs Bridge Decouple an abstraction from its implementation so that

    the two can vary independently.
  24. jgs 564 00010010 Bridge

  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();
  26. jgs 564 00010010 Strategy vs Bridge

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

  29. jgs CSE 564 Software Design Javier Gonzalez-Sanchez, Ph.D. javiergs@asu.edu 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.