Created
July 17, 2017 20:32
-
-
Save abgoswam/0c1eda342d4631e35f3227e3ccc41008 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 -*- | |
""" | |
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