3 Ways to Implement Factory Pattern
Explanation Video Link on Youtube
# Use if-else
import abc
class Car(object):
@staticmethod
@abc.abstractmethod
def calculate_cost(tank_size):
return
class HybridCar(Car):
def calculate_cost(tank_size):
return 5 * 0.5 * tank_size + 2 * 0.5 * tank_size;
class GasCar(Car):
def calculate_cost(tank_size):
return 5 * tank_size;
def get_concrete_classes(car_type):
if car_type == "GasCar":
return GasCar
elif car_type == "HybridCar":
return HybridCar
print(get_concrete_classes(car_type = "GasCar").calculate_cost(12))
print(get_concrete_classes(car_type = "HybridCar").calculate_cost(12))Last updated
Was this helpful?