Slide 1

Slide 1 text

Dr. Javier Gonzalez-Sanchez [email protected] www.javiergs.info o ffi ce: 14 -227 CSC 305 Individual Software Design and Development Lecture 17. Chain of Responsabily and Builder

Slide 2

Slide 2 text

Previously

Slide 3

Slide 3 text

GoF Patterns 3

Slide 4

Slide 4 text

Observer 4

Slide 5

Slide 5 text

Singleton 5

Slide 6

Slide 6 text

Composite 6

Slide 7

Slide 7 text

Strategy 7

Slide 8

Slide 8 text

Examples

Slide 9

Slide 9 text

Logger 9

Slide 10

Slide 10 text

Desktop 10

Slide 11

Slide 11 text

Streams 11

Slide 12

Slide 12 text

Chain of Responsibility

Slide 13

Slide 13 text

Chain of Responsibility • Avoid coupling the sender of a request to its receiver by giving more th a n one object a ch a nce to h a ndle the request. 13

Slide 14

Slide 14 text

Chain of Responsibility 14

Slide 15

Slide 15 text

Chain of Responsibility vs Composite vs Decorator 15

Slide 16

Slide 16 text

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 17

Slide 17 text

Handler abstract class Handler { public abstract void handleRequest(int request); } class ConcreteHandler1 extends Handler { public void handleRequest(int request) { if (request >= 0 && request < 10) { // code here… } else { // nothing can be done :( } } } 17

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

Fluent-Builder

Slide 20

Slide 20 text

Fluent-Builder • The Fluent Builder is a builder whose design relies on method chaining. In doing so, it a ims to promote code legibility. 20

Slide 21

Slide 21 text

Example 21

Slide 22

Slide 22 text

Fluent-Builder import java.awt.*; public class Client { public static void main(String[] args) { FluentBuilder builder = new BoxFluentBuilder() .setColor(Color.RED) .setSize(20) .setX(10) .setY(20); Product box = ((BoxFluentBuilder)builder).get(); } } 22

Slide 23

Slide 23 text

Fluent-Builder import java.awt.Color; public interface FluentBuilder { public Builder setColor(Color color); public Builder setSize(int size); public Builder setX(int x); public Builder setY(int y); } 23

Slide 24

Slide 24 text

Fluent-Builder import java.awt.*; public class BoxFluentBuilder implements FluentBuilder { private Color color; private int size, x, y; public Builder setColor (Color color) { this.color = color; return this; } public Builder setSize(int size) { this.size = size; return this; } public Builder setX(int x) { this.x = x; return this; } public Builder setY(int y) { this.y = y; return this; } public Box get() { return new Box (color, size, x, y); } } 24

Slide 25

Slide 25 text

GoF Patterns 25

Slide 26

Slide 26 text

Questions 26

Slide 27

Slide 27 text

Assignment 03. GUI for Hanoi

Slide 28

Slide 28 text

Homework You do NOT need all the patterns! Use the tools that better fit

Slide 29

Slide 29 text

Create a GUI for the Game 29 That was a good move! That is not good, go back Do you want a hint? Move from pole (A,B,C): To pole (A,B,C):

Slide 30

Slide 30 text

ToDo List Using the GUI provided, integr a te the text-b a sed g a me you cre a ted for Assignment 1 into the interf a ce. Your implement a tion should include the following fe a tures: • Dr a g- a nd-Drop Disk Movement: Implement dr a g- a nd-drop function a lity to a llow users to move disks a cross poles. • Re a l-Time Move V a lid a tion: Ensure re a l-time checks a re in pl a ce to prevent inv a lid moves, such a s pl a cing a l a rger disk on top of a sm a ller one on the s a me pole. • Hint System for Assist a nce: Provide a hint fe a ture to a ssist the user by suggesting the next optim a l move when requested. • Applic a tion of Design P a tterns: Utilize a ppropri a te softw a re design p a tterns to cre a te a structured a nd e ff icient solution. • The f in a l GUI should o ff er a n intuitive experience, m a king it e a sy for users to inter a ct with the g a me while enforcing a ll g a me rules e ff ectively. • 30

Slide 31

Slide 31 text

CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D. [email protected] Summer 2024 Copyright. These slides can only be used as study material for the class CSC305 at Cal Poly. They cannot be distributed or used for another purpose.