mardi 8 septembre 2020

Aggregation or Dependency in the Simple Factory (UML)

This question is a two-parter to avoid stacking multiple questions in one post.

In a course I'm taking, a PizzaStore uses a simplePizzaFactory class that handles concrete pizza instantiation, described with the following diagram (as provided in the course material): enter image description here

Code that I re-wrote in python:

# Pizza's superclass and it's subclasses are defined elswhere

class SimplePizzaFactory:
    def create_pizza(self,type_of_pizza):
        if type_of_pizza == "cheese":
            pizza = CheesePizza()
        elif type_of_pizza == "pepperoni":
            pizza = PepperoniPizza()

        elif type_of_pizza == "clam":
            pizza = ClamPizza()

        elif type_of_pizza == "viggie":
            pizza = ViggiePizza()
        else:
            raise Exception("You need to specify a type of pizza.")
        
        return pizza


class PizzaStore:
    def __init__(self, pizza_factory_obj):
        self.pizza_factory_obj = pizza_factory_obj

    def order_pizza(self,type_of_pizza):
        type_of_pizza = type_of_pizza.lower() 
        pizza = self.pizza_factory_obj.create_pizza(type_of_pizza) 
        pizza.prepare()
        pizza.bake()
        pizza.box()
        return pizza

print("========================================================")
factory = SimplePizzaFactory()
store = PizzaStore(factory)
store.order_pizza("Cheese")
store.order_pizza("viggie")

Question:

Generalized by (from the course material):

enter image description here

I'd understand that the first arrow is aggregation (since an object of the simplePizzaFactory is created and sent to the PizzaStore as an argument) but how is the second arrow is also aggregation? shouldn't it make more sense to be a dotted dependency arrow?

I'd appreciate more clarification on this part and on my understanding as well if I was incorrect regarding the first arrow. Any comments on the code would be also appreciated

Aucun commentaire:

Enregistrer un commentaire