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

CSC305 Lecture 17

CSC305 Lecture 17

Individual Software Design and Development
Chain of Responsibility and Fluent Builder
(202410)

Javier Gonzalez-Sanchez

October 29, 2024
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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<requests.lenght; i++) { h1.handleRequest(request[i]); } 16
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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):
  11. 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
  12. 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.