Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs Previously …

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

jgs 564 00010010 Chain of Responsibility

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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); } } }

Slide 9

Slide 9 text

jgs 564 00010010 Chain of Responsibility vs Composite vs Decorator

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

jgs 564 00010010 Strategy

Slide 12

Slide 12 text

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();

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

jgs 564 00010010 Composite

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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);

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

jgs 564 00010010 Adapter

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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); } }

Slide 21

Slide 21 text

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); } }

Slide 22

Slide 22 text

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”); } }

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

jgs 564 00010010 Bridge

Slide 25

Slide 25 text

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();

Slide 26

Slide 26 text

jgs 564 00010010 Strategy vs Bridge

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

jgs 564 00010010 Questions

Slide 29

Slide 29 text

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.