Created
September 23, 2014 13:14
-
-
Save minsOne/2a2b1594695b3b9c9f4c to your computer and use it in GitHub Desktop.
Abstract_Factory_Pattern
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
| protocol Button { | |
| func paint() | |
| } | |
| protocol GUIFactory { | |
| func createButton() -> Button | |
| } | |
| class WinButton: Button { | |
| func paint() { | |
| println("Painting WinButton") | |
| } | |
| } | |
| class OSXButton: Button { | |
| func paint() { | |
| println("Painting OSXButton") | |
| } | |
| } | |
| class WinFactory: GUIFactory { | |
| func createButton() -> Button { | |
| return WinButton() | |
| } | |
| } | |
| class OSXFactory:GUIFactory { | |
| func createButton() -> Button { | |
| return OSXButton() | |
| } | |
| } | |
| class Application { | |
| init(factory: GUIFactory) { | |
| let button: Button = factory.createButton() | |
| button.paint() | |
| } | |
| } | |
| //**Usage** | |
| func createOsSpecificFactory(OS: Int) -> GUIFactory { | |
| if OS == 0 { | |
| return WinFactory() | |
| } else { | |
| return OSXFactory() | |
| } | |
| } | |
| let someApplication: Application = Application( factory: createOsSpecificFactory(0)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment