Skip to content

Instantly share code, notes, and snippets.

@abgoswam
Created July 17, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abgoswam/0c1eda342d4631e35f3227e3ccc41008 to your computer and use it in GitHub Desktop.
Save abgoswam/0c1eda342d4631e35f3227e3ccc41008 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 17 13:13:26 2017
@author: agoswami
"""
class Car(object):
@staticmethod
def factory(type):
if type == "Racecar":
return Racecar()
if type == "Van":
return Van()
assert 0, "Bad car creation: " + type
# factory = staticmethod(factory)
class Racecar(Car):
def drive(self): print("Racecar driving.")
class Van(Car):
def drive(self): print("Van driving.")
# Create object using factory.
obj = Car.factory("Racecar")
obj.drive()
obj = Car.factory("Van")
obj.drive()
obj = Car.factory("Racecar")
obj.drive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment