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,21 +1,30 @@
package abstractfactory;
/** Client - uses the factory without knowing concrete classes */
public class Application {
private final Button button;
private final Checkbox checkbox;
public Application(GUIFactory factory) {
button = factory.createButton();
checkbox = factory.createCheckbox();
// The client holds references to the Abstract Factory and Abstract Products.
// It never imports LightButton, DarkButton, or any concrete class.
private final UIFactory factory;
private Button submitButton;
private TextField usernameField;
public Application(UIFactory factory) {
this.factory = factory; // theme is injected; client doesn't decide
}
public void buildUI() {
// Ask the factory — get consistent products from the same family
submitButton = factory.createButton();
usernameField = factory.createTextField();
}
public void render() {
button.render();
checkbox.render();
System.out.println("Rendering login screen:");
usernameField.render();
submitButton.render();
}
public void simulateClick() {
button.onClick();
public void simulateSubmit() {
submitButton.onClick();
}
}

View File

@@ -1,6 +1,7 @@
package abstractfactory;
// Every button, regardless of theme, must be able to render and handle clicks
public interface Button {
void render();
void onClick();
void render(); // draw on screen
void onClick(); // respond to a click event
}

View File

@@ -1,5 +0,0 @@
package abstractfactory;
public interface Checkbox {
void render();
}

View File

@@ -0,0 +1,11 @@
package abstractfactory;
public class DarkButton implements Button {
@Override
public void render() {
System.out.println("[Dark Button] Charcoal background, white text, no border");
}
@Override
public void onClick() {
System.out.println("[Dark Button] Bright highlight ripple effect");
}
}

View File

@@ -0,0 +1,10 @@
package abstractfactory;
public class DarkTextField implements TextField {
private String value = "";
@Override
public void render() {
System.out.println("[Dark TextField] Dark gray background, light placeholder text");
}
@Override
public String getValue() { return value; }
}

View File

@@ -0,0 +1,14 @@
package abstractfactory;
public class DarkThemeFactory implements UIFactory {
@Override
public Button createButton() {
return new DarkButton(); // Only Dark products come from this factory
}
@Override
public TextField createTextField() {
return new DarkTextField();
}
}

View File

@@ -1,7 +0,0 @@
package abstractfactory;
/** Abstract Factory - creates families of related UI components */
public interface GUIFactory {
Button createButton();
Checkbox createCheckbox();
}

View File

@@ -0,0 +1,11 @@
package abstractfactory;
public class LightButton implements Button {
@Override
public void render() {
System.out.println("[Light Button] White background, dark text, 1px gray border");
}
@Override
public void onClick() {
System.out.println("[Light Button] Subtle gray ripple effect");
}
}

View File

@@ -0,0 +1,10 @@
package abstractfactory;
public class LightTextField implements TextField {
private String value = "";
@Override
public void render() {
System.out.println("[Light TextField] White background, dark placeholder text");
}
@Override
public String getValue() { return value; }
}

View File

@@ -0,0 +1,14 @@
package abstractfactory;
public class LightThemeFactory implements UIFactory {
@Override
public Button createButton() {
return new LightButton(); // Only Light products come from this factory
}
@Override
public TextField createTextField() {
return new LightTextField();
}
}

View File

@@ -1,6 +0,0 @@
package abstractfactory;
public class MacButton implements Button {
@Override public void render() { System.out.println("[Mac] Rendering button with rounded corners"); }
@Override public void onClick() { System.out.println("[Mac] Button clicked - glow animation"); }
}

View File

@@ -1,5 +0,0 @@
package abstractfactory;
public class MacCheckbox implements Checkbox {
@Override public void render() { System.out.println("[Mac] Rendering checkbox with rounded tick box"); }
}

View File

@@ -1,6 +0,0 @@
package abstractfactory;
public class MacFactory implements GUIFactory {
@Override public Button createButton() { return new MacButton(); }
@Override public Checkbox createCheckbox() { return new MacCheckbox(); }
}

View File

@@ -1,22 +1,16 @@
package abstractfactory;
// Entry point: the ONLY place that knows the theme
public class Main {
public static void main(String[] args) {
System.out.println("=== Abstract Factory Pattern Demo ===\n");
String os = System.getProperty("os.name", "Windows").toLowerCase();
GUIFactory factory = os.contains("mac") ? new MacFactory() : new WindowsFactory();
System.out.println("Detected OS family: " + (os.contains("mac") ? "Mac" : "Windows"));
System.out.println();
// Production: read theme from config or OS preference
String theme = System.getProperty("app.theme", "light");
UIFactory factory = "dark".equals(theme) ? new DarkThemeFactory() : new LightThemeFactory();
Application app = new Application(factory);
app.buildUI();
app.render();
app.simulateClick();
System.out.println("\n--- Forcing Mac UI ---");
Application macApp = new Application(new MacFactory());
macApp.render();
macApp.simulateClick();
app.simulateSubmit();
}
}

View File

@@ -0,0 +1,7 @@
package abstractfactory;
// Every text field must be able to render and return its value
public interface TextField {
void render();
String getValue();
}

View File

@@ -0,0 +1,13 @@
package abstractfactory;
public interface UIFactory {
// Factory method for buttons — returns the abstract type, not LightButton or DarkButton
Button createButton();
// Factory method for text fields — same principle
TextField createTextField();
// Adding a new product type (e.g., Checkbox) means adding a method here
// AND implementing it in every Concrete Factory. This is the one cost of the pattern.
}

View File

@@ -1,6 +0,0 @@
package abstractfactory;
public class WindowsButton implements Button {
@Override public void render() { System.out.println("[Windows] Rendering button with square corners"); }
@Override public void onClick() { System.out.println("[Windows] Button clicked - raised border effect"); }
}

View File

@@ -1,5 +0,0 @@
package abstractfactory;
public class WindowsCheckbox implements Checkbox {
@Override public void render() { System.out.println("[Windows] Rendering checkbox with square tick box"); }
}

View File

@@ -1,6 +0,0 @@
package abstractfactory;
public class WindowsFactory implements GUIFactory {
@Override public Button createButton() { return new WindowsButton(); }
@Override public Checkbox createCheckbox() { return new WindowsCheckbox(); }
}