Sync Factory Method, Abstract Factory, Builder, Prototype to Java 25; fix post/code mismatches

This commit is contained in:
2026-06-23 11:00:02 +05:30
parent 2f684bf3d7
commit 2fbf89875b
51 changed files with 634 additions and 337 deletions

View File

@@ -1,20 +1,42 @@
package builder;
public class Main {
public static void main(String[] args) {
System.out.println("=== Builder Pattern Demo ===\n");
Director director = new Director();
// Simple GET — only the required field
HttpRequest ping = new HttpRequest.Builder()
.url("https://api.example.com/health")
.build();
House starter = director.buildStarter(new House.Builder());
System.out.println("Starter home : " + starter);
System.out.println("Simple GET: " + ping);
House luxury = director.buildLuxury(new House.Builder());
System.out.println("Luxury home : " + luxury);
// Full POST with authentication and retry
HttpRequest post = new HttpRequest.Builder()
.url("https://api.example.com/orders")
.method("POST")
.body("{\"item\":\"book\",\"qty\":1}")
.header("Content-Type", "application/json")
.header("Authorization", "Bearer eyJ...")
.timeoutMs(10_000) // 10 seconds — name is self-documenting
.maxRetries(3)
.build();
// Client builds a custom house directly without the Director
House custom = new House.Builder()
.rooms(4).floors(2).garden(true).roofType("mansard").build();
System.out.println("Custom home : " + custom);
System.out.println("POST: " + post);
// build() throws if url is missing — validation at construction time
try {
HttpRequest bad = new HttpRequest.Builder().build(); // no URL!
} catch (IllegalStateException e) {
System.out.println("Caught: " + e.getMessage()); // url() is required
}
// Using the Director for the two standard presets defined above
HttpClientConfig config = new HttpClientConfig();
HttpRequest healthCheck = config.buildHealthCheck("https://api.example.com");
System.out.println("Health check: " + healthCheck);
HttpRequest order = config.buildResilientPost("https://api.example.com/orders", "{\"qty\":1}");
System.out.println("Order: " + order);
}
}