Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# -*- 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