Created
December 29, 2015 18:54
-
-
Save codinko/6949fa3a4fe830220665 to your computer and use it in GitHub Desktop.
AbstractFactoryPattern
This file contains 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
//GuiFactory example | |
//Abstract Product | |
interface Button { | |
void paint(); | |
} | |
//Abstract Product | |
interface Label { | |
void paint(); | |
} | |
//Abstract Factory | |
interface GUIFactory { | |
Button createButton(); | |
Label createLabel(); | |
} | |
//Concrete Factory | |
class WinFactory implements GUIFactory { | |
public Button createButton() { | |
return new WinButton(); | |
} | |
public Label createLabel() { | |
return new WinLabel(); | |
} | |
} | |
//Concrete Factory | |
class OSXFactory implements GUIFactory { | |
public Button createButton() { | |
return new OSXButton(); | |
} | |
public Label createLabel() { | |
return new OSXLabel(); | |
} | |
} | |
//Concrete Product | |
class OSXButton implements Button { | |
public void paint() { | |
System.out.println("I'm an OSXButton"); | |
} | |
} | |
//Concrete Product | |
class WinButton implements Button { | |
public void paint() { | |
System.out.println("I'm a WinButton"); | |
} | |
} | |
//Concrete Product | |
class OSXLabel implements Label { | |
public void paint() { | |
System.out.println("I'm an OSXLabel"); | |
} | |
} | |
//Concrete Product | |
class WinLabel implements Label { | |
public void paint() { | |
System.out.println("I'm a WinLabel"); | |
} | |
} | |
//Client application is not aware about the how the product is created. Its only responsible to give a name of | |
//concrete factory | |
class Application { | |
public Application(GUIFactory factory) { | |
Button button = factory.createButton(); | |
Label label = factory.createLabel(); | |
button.paint(); | |
label.paint(); | |
} | |
} | |
public class ApplicationRunner { | |
public static void main(String[] args) { | |
new Application(createOsSpecificFactory()); | |
} | |
public static GUIFactory createOsSpecificFactory() { | |
String osname = "windows"; // get the input dynamically from user | |
if(osname != null && osname.contains("windows")) | |
return new WinFactory(); | |
else | |
return new OSXFactory(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment