Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

jgs 564 00010001 GoF Patterns

Slide 3

Slide 3 text

jgs Factory Wrapper of a constructor One entire object to be built in a single method call

Slide 4

Slide 4 text

jgs 564 00010001 Abstract Factory

Slide 5

Slide 5 text

jgs 564 00010001 Products public interface Shape { void draw(); } public class RoundedRectangle implements Shape { public void draw() { System.out.println("Inside RoundedRectangle/draw() method."); } } public class RoundedSquare implements Shape { public void draw() { System.out.println("Inside RoundedSquare/draw() method."); } } public class Rectangle implements Shape { public void draw() { System.out.println("Inside Rectangle/draw() method."); } }

Slide 6

Slide 6 text

jgs 564 00010001 Factories public abstract class AbstractFactory { Shape getShape(String shapeType) ; } public class ShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new Rectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new Square(); } return null; } }

Slide 7

Slide 7 text

jgs 564 00010001 Factories public class RoundedShapeFactory extends AbstractFactory { public Shape getShape(String shapeType){ if(shapeType.equalsIgnoreCase("RECTANGLE")){ return new RoundedRectangle(); }else if(shapeType.equalsIgnoreCase("SQUARE")){ return new RoundedSquare(); } return null; } }

Slide 8

Slide 8 text

jgs 564 00010001 Client 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(); } }

Slide 9

Slide 9 text

jgs Test Yourselves

Slide 10

Slide 10 text

jgs 564 00010001 Workspace.java (part 1 / 2)

Slide 11

Slide 11 text

jgs 564 00010001 User D B A C

Slide 12

Slide 12 text

jgs 564 00010001 The System D B A C random

Slide 13

Slide 13 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 14

Slide 14 text

jgs 564 00010001 Chain of Responsibility

Slide 15

Slide 15 text

jgs 564 00010001 Chain of Responsibility // Setup Chain of Responsibility Handler h1 = new ConcreteHandler2(); Handler h2 = new ConcreteHandler2(); Handler h3 = new ConcreteHandler1(); 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 16

Slide 16 text

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

Slide 17

Slide 17 text

jgs 564 00010001 Handler class ConcreteHandler2 extends Handler { protected Handler successor; public void setSuccessor(Handler successor) { this.successor = successor; } public override void HandleRequest(int request) { if (request >= 10 && request < 20) { // code here } else if (successor != null) { successor.HandleRequest(request); } } }

Slide 18

Slide 18 text

jgs 564 00010001 Chain of Responsibility vs Composite vs Decorator

Slide 19

Slide 19 text

jgs Test Yourselves

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

jgs 564 00010001 Questions

Slide 22

Slide 22 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.