C/O - Abstract Factory
- Category: creational
- Type: object
- Motivation: one factory makes family products
do {
// Client
let doorFactory = WoodenDoorFactory()
let door = doorFactory.makeDoor()
let expert = doorFactory.makeFittingExpert()
door.getDescription()
expert.getDescription()
}
protocol Door
{
func getDescription()
}
class WoodenDoor: Door
{
func getDescription()
{
print("I am a wooden door")
}
}
protocol FittingExpert
{
func getDescription()
}
class Carpenter: FittingExpert
{
func getDescription()
{
print("I can only fit wooden doors")
}
}
protocol DoorFactory
{
func makeDoor(): Door
func makeFittingExpert(): FittingExpert
}
class WoodenDoorFactory: DoorFactory
{
func makeDoor() -> WoodenDoor
{
return WoodenDoor()
}
func makeFittingExpert() -> Carpenter
{
return Carpenter()
}
}