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 scripts.logging.logging import logger
# Product class that we want to build with the builder pattern
class Meal:
......@@ -60,11 +62,17 @@ class ComboMealBuilder(MealBuilder):
# Director class that uses the builder to build the meal
class MealAssembler:
def __init__(self, builder: MealBuilder):
try:
self.builder = builder
except Exception as e:
logger.error(e)
def assemble_meal(self):
try:
self.builder.add_drink()
self.builder.add_payasam()
self.builder.add_side()
self.builder.add_dessert()
return self.builder.get_meal()
except Exception as e:
logger.error(e)
from fastapi import APIRouter
from scripts.constants.endpoints import EndPoints
from scripts.core.handlers.shape_handler import CircleFactory, AreaCalculator, RectangleFactory
from scripts.logging.logging import logger
shape = APIRouter()
......@@ -9,13 +10,19 @@ shape = APIRouter()
# Register endpoints that use the abstract factory
@shape.get(EndPoints.circle_area, tags=["Area of a Circle"])
async def circle_area(radius: float):
try:
factory = CircleFactory()
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"])
async def rectangle_area(width: float, height: float):
try:
factory = RectangleFactory()
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