Created
March 2, 2011 18:46
-
-
Save gennad/851450 to your computer and use it in GitHub Desktop.
Abstract Factory - GoF
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
# -*- coding: utf-8 -*- | |
class AbstractCarFactory: | |
"""Абстрактная фабрика""" | |
def createCar(self): | |
raise NotImplementedError() | |
class FordFactory(AbstractCarFactory): | |
"""Конкретная фабрика A""" | |
def createCar(self): | |
return Ford() | |
class ToyotaFactory(AbstractCarFactory): | |
"""Конкретная фабрика Б""" | |
def createCar(self): | |
return Toyota() | |
class Car: | |
"""Абстрактный продукт""" | |
def printName(self): | |
return NotImplementedError() | |
class Ford(Car): | |
"""Конкретный продукт А""" | |
def printName(self): | |
print 'Ford Focus' | |
class Toyota(Car): | |
"""Конкретный продукт Б""" | |
def printName(self): | |
print 'Toyota Corolla' | |
location = 'Japan' | |
if location == 'Japan': | |
factory = ToyotaFactory() | |
else: | |
factory = FordFactory() | |
car = factory.createCar() | |
car.printName() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment