Commit cb076722 authored by arjun.b's avatar arjun.b

abstract factory and builder implementation in fastapi

parent a30a0200
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from scripts.logging.logging import logger
# Product class that we want to build with the builder pattern # Product class that we want to build with the builder pattern
class Meal: class Meal:
...@@ -60,11 +62,17 @@ class ComboMealBuilder(MealBuilder): ...@@ -60,11 +62,17 @@ class ComboMealBuilder(MealBuilder):
# Director class that uses the builder to build the meal # Director class that uses the builder to build the meal
class MealAssembler: class MealAssembler:
def __init__(self, builder: MealBuilder): def __init__(self, builder: MealBuilder):
self.builder = builder try:
self.builder = builder
except Exception as e:
logger.error(e)
def assemble_meal(self): def assemble_meal(self):
self.builder.add_drink() try:
self.builder.add_payasam() self.builder.add_drink()
self.builder.add_side() self.builder.add_payasam()
self.builder.add_dessert() self.builder.add_side()
return self.builder.get_meal() self.builder.add_dessert()
return self.builder.get_meal()
except Exception as e:
logger.error(e)
from fastapi import APIRouter from fastapi import APIRouter
from scripts.constants.endpoints import EndPoints from scripts.constants.endpoints import EndPoints
from scripts.core.handlers.shape_handler import CircleFactory, AreaCalculator, RectangleFactory from scripts.core.handlers.shape_handler import CircleFactory, AreaCalculator, RectangleFactory
from scripts.logging.logging import logger
shape = APIRouter() shape = APIRouter()
...@@ -9,13 +10,19 @@ shape = APIRouter() ...@@ -9,13 +10,19 @@ shape = APIRouter()
# Register endpoints that use the abstract factory # Register endpoints that use the abstract factory
@shape.get(EndPoints.circle_area, tags=["Area of a Circle"]) @shape.get(EndPoints.circle_area, tags=["Area of a Circle"])
async def circle_area(radius: float): async def circle_area(radius: float):
factory = CircleFactory() try:
calculator = AreaCalculator(factory, radius) factory = CircleFactory()
return {"area": calculator.calculate_area()} calculator = AreaCalculator(factory, radius)
return {"area": calculator.calculate_area()}
except Exception as e:
logger.error(e)
@shape.get(EndPoints.rectangle_area, tags=["Area of a Rectangle"]) @shape.get(EndPoints.rectangle_area, tags=["Area of a Rectangle"])
async def rectangle_area(width: float, height: float): async def rectangle_area(width: float, height: float):
factory = RectangleFactory() try:
calculator = AreaCalculator(factory, width, height) factory = RectangleFactory()
return {"area": calculator.calculate_area()} calculator = AreaCalculator(factory, width, height)
return {"area": calculator.calculate_area()}
except Exception as e:
logger.error(e)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment