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

CSC310 Lecture 08

Avatar for Javier Gonzalez-Sanchez Javier Gonzalez-Sanchez PRO
September 27, 2026
18

CSC310 Lecture 08

Builder and Factory
(20260925)

Avatar for Javier Gonzalez-Sanchez

Javier Gonzalez-Sanchez PRO

September 27, 2026

Transcript

  1. CSC 3100 Software Engineering Lecture 08. Builder and Factory Dr.

    Javier Gonzalez-Sanchez [email protected] www.javiergs.info ffi o ce: 14 -227
  2. Agenda • This is Week 06 • Week 08 -

    Mond y October 12 - Midterm Review a a 2 a • Wednesd y October 14 - Midterm Ex m
  3. What is a design pattern? • A design situ tion

    ppe rs repe tedly cross pplic tions nd dom ins. • A reus ble design ide —not code to copy nd not libr ry. • The p ttern n mes roles nd rel tionships th t solve the problem. • A p ttern h s tr deo s. Use it bec use the problem exists • P ttern = voc bul ry for discussing design decision a a a a a a a a a a a a a a a a a ff a a a a a a a a a a a 4 a • Ask: Wh t design problem do I h ve?
  4. Patterns in our Sprint 2 libraries Builder XChart Construct a

    configurable chart Factory JFreeChart / Jzy3D Centralize object creation
  5. XYChart Map the roles XYChart chart = new XYChartBuilder() .width(800)

    .height(600) .title("Pose Data") .xAxisTitle("Time") .yAxisTitle("Value") .build(); Builder → XYChartBuilder Product → XYChart Steps → width(), height(), title(), ... Finalization → build() Intent Problem: creating an object requires many optional configuration choices. Why Builder here? Readable optional configuration • Avoid a huge constructor (for the Interface!) 8 Separate step-by-step construction from the finished object. The same construction process can produce different configurations.
  6. UserPro 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; } 10 f } public String toString() { return String.format( "UserProfile: { Username: %s, Email: %s, Phone: %s, Address: %s, Age: %d }", username, email, phoneNumber, address, age ); }
  7. UserPro 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; } 11 f public UserProfileBuilder email(String email) { this.email = email; return this; }
  8. UserPro 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; } 12 f } public UserProfile build() { return new UserProfile(username, email, phoneNumber, address, age); }
  9. 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"); } 13 } UserProfile user2 = builder.address("456 Elm St, LA").age(30).build(); System.out.println(user1); System.out.println(user2);
  10. Factory Pattern Intent Problem: client code should not be responsible

    for detailed object construction. Move creation behind a factory operation. Clients request a product; the factory decides how to construct it. 15
  11. Factory → JFreeChart and Jzy3D 16 JFreeChart chart = ChartFactory.createXYLineChart(...);

    IChartFactory factory = ...; Chart chart = factory.newChart(...); JFreeChart Jzy3D Client asks ChartFactory for a chart. Construction details stay outside the client. Factory abstractions support different chart/ toolkit environments. This naturally introduces the Abstract Factory idea.
  12. Lab

  13. CSC 3100 Software Engineering Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2026

    Copyright. These slides may be used only as study material for CSC 5100 within the California State University system. They may not be distributed or used for any other purpose.