Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE564 Lecture 18

CSE564 Lecture 18

Software Design
Additional Patterns II
(202111)

Javier Gonzalez-Sanchez PRO

September 18, 2020

Transcript

29 slides
  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
  2. jgs 564 00010010 The System D B A C D

    B A C Clustering TSP -Nearest Neighbor
  3. 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.
  4. 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]); }
  5. jgs 564 00010010 Handler abstract class Handler { protected Handler

    successor; public void setSuccessor(Handler successor) { this.successor = successor; } public abstract void HandleRequest(int request); }
  6. 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); } } }
  7. 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();
  8. jgs Composite Compose objects into tree structures to represent part-whole

    hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly
  9. jgs 564 00010010 Composite vs Decorator § The difference is

    more one of purpose than implementation
  10. 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);
  11. 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); } }
  12. 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); } }
  13. 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”); } }
  14. 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();
  15. 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
  16. 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.

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming