Created
October 7, 2018 15:59
-
-
Save ZoranPandovski/b04e865dd3631844261b1d817ffdef5c to your computer and use it in GitHub Desktop.
Factory method pattern in Python
This file contains 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
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