Skip to content

Instantly share code, notes, and snippets.

@gennad
Created March 2, 2011 18:46
Show Gist options
  • Save gennad/851450 to your computer and use it in GitHub Desktop.
Save gennad/851450 to your computer and use it in GitHub Desktop.
Abstract Factory - GoF
# -*- 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