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

CSC305 Summer Lecture 10

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

CSC305 Summer Lecture 10

Individual Software Design and Development
Builder and Chain of Responsabilities
(202508)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

August 16, 2025
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 | Summer Version Lecture 10. Builder + Chain of Responsibility
  2. Fluent-Builder • The Fluent Builder is a builder whose design

    relies on method chaining. In doing so, it a ims to promote code legibility. 5
  3. UserPro f ile class UserProfile { private final String username;

    private final String email; private final String phoneNumber; private final String address; private final int age; public UserProfile(String username, String email, String phoneNumber, String address, int age) { this.username = username; this.email = email; this.phoneNumber = phoneNumber; this.address = address; this.age = age; } public String toString() { return String.format( "UserProfile: { Username: %s, Email: %s, Phone: %s, Address: %s, Age: %d }", username, email, phoneNumber, address, age ); } } 6
  4. FluentBuilderExample public class FluentBuilderExample { public static void main(String[] args)

    { // Example 1: Creating a user in a single line UserProfile user1 = new UserProfileBuilder("alice123") .email("[email protected]") .phoneNumber("555-1234") .address("123 Main St, NY") .age(25) .build(); // Example 2: Building the object step by step UserProfileBuilder builder = new UserProfileBuilder("bob456"); builder.email("[email protected]").phoneNumber("555-5678"); UserProfile user2 = builder.address("456 Elm St, LA").age(30).build(); System.out.println(user1); System.out.println(user2); } } 7
  5. UserPro f ile class UserProfileBuilder { private final String username;

    private String email = "Not provided"; private String phoneNumber = "Not provided"; private String address = "Not provided"; private int age = 18; public UserProfileBuilder(String username) { this.username = username; } public UserProfileBuilder email(String email) { this.email = email; return this; } 8
  6. UserPro f ile public UserProfileBuilder phoneNumber(String phoneNumber) { this.phoneNumber =

    phoneNumber; return this; } public UserProfileBuilder address(String address) { this.address = address; return this; } public UserProfileBuilder age(int age) { if (age < 13) throw new IllegalArgumentException("Age must be 13 or older."); this.age = age; return this; } public UserProfile build() { return new UserProfile(username, email, phoneNumber, address, age); } } 9
  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(); } } 11
  8. Fluent-Builder import java.awt.Color; public interface FluentBuilder { public FluentBuilder setColor(Color

    color); public FluentBuilder setSize(int size); public FluentBuilder setX(int x); public FluentBuilder setY(int y); } 12
  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 FluentBuilder setSize(int size) { this.size = size; return this; } public FluentBuilder setX(int x) { this.x = x; return this; } public FluentBuilder setY(int y) { this.y = y; return this; } public Box get() { return new Box (color, size, x, y); } } 13
  10. 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. 15
  11. 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]); } 17
  12. 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 :( } } } 18
  13. 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); } } } 19
  14. PlantUML 24 @startuml !pragma layout smetana class Foo class Bar

    Foo -|> Bar @enduml Always the first lines. Always the last line class interface abstract class <|-- extends <|.. realization *— composition o-- aggregation --> association ..>. dependency
  15. Lab

  16. Lab 31 1.Retrieve all files. 3. For each file, create

    a “class” box (ignore the actual file contents for now). 5. Connect all “class” boxes to each other using ..> (dependency).
  17. CSC 305 Individual Software Design and Development Javier Gonzalez-Sanchez, Ph.D.

    [email protected] Summer 2025 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.