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?
.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.
{ // 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);
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.
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.