mardi 11 février 2020

Python: interaction between large classes

I have 4 classes that handle logic for a large project. I have Product that clients buys and i have to bill them. Nevertheless the price of those products is varying greatly according to many variables. I have create a class PriceGenerator that handles the pricing of the products, an Inventory class that checks if the 'Product' is still available and a 'Cart' class that contains a list of 'Product' for a total bill if the client buys many 'Product'

Class PriceGenerator:
    def get_price(*args)

Class Product:
    def prod_bill()

Class Inventory:
    def get_inventory(*args)

Class Cart:
    self.list_product = [product1, product2, product3,...]
    def cart_bill(*args)

my first option:

pg = PriceGenerator()
pd = Product()
inv = Inventory()
cart = Cart()

I could pass the PriceGenerator and Inventory as argument of Cart

def cart_bill(pg, inv, amount):
    bill = 0
    for prod in self.list_product:
        px = prod.prod_bill(pg)
        bill_p = px * min(amount, inv.get_inventory(product_args))
        bill += bill_p

Obviously as the number of methods grows in Product, it becomes very complicated to keep track of all the arguments you have to pass. you pass PriceGenerator and Inventory to Cart that are then passed to the product prod.prod_bill(pg), all those nested dependencies are very cumbersome to pass through all the objects and it makes the code much more complicated.

I could call pg and inv without passing it as arguments for example in Product as a global variable

def produce_bill(self):
    price = pg.get_price(product_args)
    inventory = inv.get_inventory(product_args)

but i really don't like not knowing what are the class/arguments required, necessary for the method.

As the project grows, what design pattern would you suggest?

Aucun commentaire:

Enregistrer un commentaire