nus-cs2030 / 2021-s1

28 stars 48 forks source link

Cyclic dependency for Project 2 #363

Open qiiwen opened 3 years ago

qiiwen commented 3 years ago

Description

Hello, I have a cyclic dependency problem in my project. So I'm at level 4 of the project and I will trigger the next ServeEvent upon a DoneEvent if there are people in the queue. This complies with the correct chronological implementation of getServeTime. Is there any other way to implement this without calling ServeEvent from DoneEvent?

glennljs commented 3 years ago

Hi I actually encountered a similar issue.

The main way to resolve Cyclic/Circular dependencies is to create an intermediate class/interface to handle the relations between the two classes. In this case, it could be an EventManager class for example.

However, since there are rather stringent constraint requirements on the project (Event -> Pair<Shop,Event>), it is a bit difficult to achieve this completely. You could instead try calling the ServeEvent from your Simulator class instead of from within your DoneEvent. This will remove the dependency of DoneEvent on ServeEvent which would remove the cyclic dependency.

i.e. When you pop your Event from the PriorityQueue, if its an instance of DoneEvent, check if there are still customers in the queue...

Hope this helps!

source: https://softwareengineering.stackexchange.com/questions/306483/how-to-solve-circular-dependency

ghost commented 3 years ago

@glennljs That's what I did too! Simple and elegant solution