Created
November 30, 2023 11:53
-
-
Save kishankg/b9fc54a4dcd287eb3c03825ecd74ade9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Abstract Product: Button | |
interface Button { | |
void click(); | |
} | |
// Concrete Products: WindowsButton and MacOSButton | |
class WindowsButton implements Button { | |
@Override | |
public void click() { | |
System.out.println("Windows button clicked"); | |
} | |
} | |
class MacOSButton implements Button { | |
@Override | |
public void click() { | |
System.out.println("MacOS button clicked"); | |
} | |
} | |
// Abstract Product: Checkbox | |
interface Checkbox { | |
void check(); | |
} | |
// Concrete Products: WindowsCheckbox and MacOSCheckbox | |
class WindowsCheckbox implements Checkbox { | |
@Override | |
public void check() { | |
System.out.println("Windows checkbox checked"); | |
} | |
} | |
class MacOSCheckbox implements Checkbox { | |
@Override | |
public void check() { | |
System.out.println("MacOS checkbox checked"); | |
} | |
} | |
// Abstract Factory: GUIFactory | |
interface GUIFactory { | |
Button createButton(); | |
Checkbox createCheckbox(); | |
} | |
// Concrete Factories: WindowsFactory and MacOSFactory | |
class WindowsFactory implements GUIFactory { | |
@Override | |
public Button createButton() { | |
return new WindowsButton(); | |
} | |
@Override | |
public Checkbox createCheckbox() { | |
return new WindowsCheckbox(); | |
} | |
} | |
class MacOSFactory implements GUIFactory { | |
@Override | |
public Button createButton() { | |
return new MacOSButton(); | |
} | |
@Override | |
public Checkbox createCheckbox() { | |
return new MacOSCheckbox(); | |
} | |
} | |
// Client Code | |
public class Application { | |
private GUIFactory guiFactory; | |
private Button button; | |
private Checkbox checkbox; | |
public Application(GUIFactory guiFactory) { | |
this.guiFactory = guiFactory; | |
this.button = guiFactory.createButton(); | |
this.checkbox = guiFactory.createCheckbox(); | |
} | |
public void run() { | |
button.click(); | |
checkbox.check(); | |
} | |
public static void main(String[] args) { | |
// Creating UI components for Windows platform | |
Application windowsApp = new Application(new WindowsFactory()); | |
windowsApp.run(); | |
// Creating UI components for MacOS platform | |
Application macosApp = new Application(new MacOSFactory()); | |
macosApp.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment