Slide 1

Slide 1 text

jgs CSE 460 Software Analysis and Design Lecture 19: Connecting the Dots Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs 460 0010011 Announcement § Assignment 05 (Patterns) is due March 29. It opens at the end of the lecture

Slide 3

Slide 3 text

jgs 460 0010011 Next

Slide 4

Slide 4 text

jgs 460 0010011 Factory

Slide 5

Slide 5 text

jgs 460 0010011 Using a Factory 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 6

Slide 6 text

jgs 460 0010011 Chain of Responsibility

Slide 7

Slide 7 text

jgs 460 0010011 Using 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 8

Slide 8 text

jgs 460 0010011 Decorator

Slide 9

Slide 9 text

jgs 460 0010011 Using a Decorator class MainApp { static void main() { // Create ConcreteComponent and two Decorators ConcreteComponent c = new ConcreteComponent(); ConcreteDecoratorA d1 = new ConcreteDecoratorA(); ConcreteDecoratorB d2 = new ConcreteDecoratorB(); // Link decorators d1.SetComponent(c); d2.SetComponent(d1); d2.Operation(); } }

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

jgs 460 0010011 Adapter

Slide 12

Slide 12 text

jgs 460 0010011 Example RectangleAdapter + draw Rectangle + draw + draw

Slide 13

Slide 13 text

jgs 460 0010011 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 14

Slide 14 text

jgs 460 0010011 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 15

Slide 15 text

jgs 460 0010011 Adaptee class Rectangle { public void draw(int x, int y, int width, int height) { System.out.println("Rectangle with coordinate left-down point”); } }

Slide 16

Slide 16 text

jgs Assignment 04 Connecting the Dots

Slide 17

Slide 17 text

jgs 460 0010011 Team 1 2 3

Slide 18

Slide 18 text

jgs 460 0010011 Idea Main Factory Gift Ball Box Envelop

Slide 19

Slide 19 text

jgs 460 0010011 Idea wrapper2 Main wrapper1 Factory decoration Envelope Box Gift wrapper3 ball Supervisor Is big > 10, box Is small < 5, envelop

Slide 20

Slide 20 text

jgs 460 0010011

Slide 21

Slide 21 text

jgs 460 0010011 Main

Slide 22

Slide 22 text

jgs 460 0010011 Factory

Slide 23

Slide 23 text

jgs 460 0010011 Chain of Responsibilities

Slide 24

Slide 24 text

jgs 460 0010011 Decorator

Slide 25

Slide 25 text

jgs 460 0010011 Decorator

Slide 26

Slide 26 text

jgs 460 0010011 Observer

Slide 27

Slide 27 text

jgs 460 0010011

Slide 28

Slide 28 text

jgs CSE 460 Software Analysis and Design Javier Gonzalez-Sanchez [email protected] Fall 2020 Disclaimer. These slides can only be used as study material for the class CSE460 at ASU. They cannot be distributed or used for another purpose.