Created
February 21, 2016 10:21
-
-
Save pedrocid/c7e9c35cfb34986130b5 to your computer and use it in GitHub Desktop.
Abstract Factory Pattern Example
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
//MARK: Abstract factory pattern | |
protocol AbstractProductA{ | |
func display() -> String | |
} | |
protocol AbstractProductB{ | |
func display() -> String | |
} | |
protocol AbstractFactory{ | |
func product1() -> AbstractProductA | |
func product2() -> AbstractProductB | |
} | |
class ConcreteProductOneA: AbstractProductA { | |
func display() -> String { | |
return "Concrete product A 1" | |
} | |
} | |
class ConcreteProductOneB: AbstractProductB { | |
func display() -> String { | |
return "Concrete product B 1" | |
} | |
} | |
class ConcreteProductTwoA: AbstractProductA { | |
func display() -> String { | |
return "Concrete product A 2" | |
} | |
} | |
class ConcreteProductTwoB: AbstractProductB { | |
func display() -> String { | |
return "Concrete product B 2" | |
} | |
} | |
class ConcreteFactoryOne: AbstractFactory { | |
func product1() -> AbstractProductA { | |
return ConcreteProductOneA() | |
} | |
func product2() -> AbstractProductB { | |
return ConcreteProductOneB() | |
} | |
} | |
class ConcreteFactoryTwo: AbstractFactory { | |
func product1() -> AbstractProductA { | |
return ConcreteProductTwoA() | |
} | |
func product2() -> AbstractProductB { | |
return ConcreteProductTwoB() | |
} | |
} | |
//MARK: Use of the Abstract factory pattern | |
var factory: AbstractFactory = ConcreteFactoryOne() | |
var productA: AbstractProductA = factory.product1() | |
var productB: AbstractProductB = factory.product2() | |
print(productA.display()) | |
print(productB.display()) | |
factory = ConcreteFactoryTwo() | |
productA = factory.product1() | |
productB = factory.product2() | |
print(productA.display()) | |
print(productB.display()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment