Skip to content

Instantly share code, notes, and snippets.

@ZoranPandovski
Created October 7, 2018 15:59
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 ZoranPandovski/b04e865dd3631844261b1d817ffdef5c to your computer and use it in GitHub Desktop.
Save ZoranPandovski/b04e865dd3631844261b1d817ffdef5c to your computer and use it in GitHub Desktop.
Factory method pattern in Python
class Car:
def __init__(self, name, capacity):
self._name = name
self._capacity = capacity
def drive(self):
print("Starting the Car engine!")
class Bus:
def __init__(self, name, capacity):
self.name = name
self._capacity = capacity
def drive(self):
print("Starting the Bus engine!")
def get_vehicle(vehicle="Car"):
'''The Factory method '''
vehicles = dict(car=Car("BMW", 4), bus=Bus("Yutong", 50))
return vehicles[vehicle]
car = get_vehicle('car')
car.drive()
bus = get_vehicle('bus')
bus.drive()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment