Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created September 23, 2014 13:14
Show Gist options
  • Select an option

  • Save minsOne/2a2b1594695b3b9c9f4c to your computer and use it in GitHub Desktop.

Select an option

Save minsOne/2a2b1594695b3b9c9f4c to your computer and use it in GitHub Desktop.
Abstract_Factory_Pattern
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