nus-cs2030 / 2021-s1

28 stars 48 forks source link

cyclic dependency in Project #427

Open yixin567 opened 3 years ago

yixin567 commented 3 years ago

Description

Describe the question
I am facing cyclic dependency in the project level 4 and there's some post previously about solving it but I am still unsure of how to do it! What i did was my DoneEvent returns ServeEvent and my ServeEvent goes to DoneEvent which i think is the cause of this dependency. May i know does anyone have any easy way to solve it? thank you!!

Topic:

Specific Topics this question is related to (If Any)

Screenshots (if any):

Insert Images here if necessary

taneshin commented 3 years ago

I had the same issue as you. I moved the logic from DoneEvent into Main

yixin567 commented 3 years ago

@taneshin but how did u do that... does it mean that ur DoneEvent now does not return anything...

eman-kom commented 3 years ago

how about interfaces

the prof said something abt it in his wrap up lecture

kahehe commented 3 years ago

Done Event can just return another DoneEvent with the exact same params

just make sure that in Main, if the polled event is an instanceof DoneEvent, do not add the next event into priority queue

Hope this helps! ;)

yixin567 commented 3 years ago

@kahehe thank you!! but can i just ask right, if i shift it to the main, how do i access the variables in the event. For example if i want to get the server ID that is one of the parameter for the doneEvent.

Jcheez commented 3 years ago

Since DoneEvent is a subclass of Event, u can super ServerID from DoneEvent into Events. After that, define a getter in the Events class so that you can access the corresponding attribute

Hope this helps!

hhhhanz commented 3 years ago

I believe the DoneEvent you pulled out will contain a customer then u can use shop.find() to find the server whose id matches with the customer's server. (if u have these attributes)

kahehe commented 3 years ago

@kahehe thank you!! but can i just ask right, if i shift it to the main, how do i access the variables in the event. For example if i want to get the server ID that is one of the parameter for the doneEvent.

In Done Event class, your constructor shld already have the parameters! just use back the same params when u return a new doneevent in the constructor!

Main just checks using instanceof

descentbw commented 3 years ago

I also returned ServeEvent from DoneEvent through the Main class, so as to avoid cyclic dependencies.

taneshin commented 3 years ago

@taneshin but how did u do that... does it mean that ur DoneEvent now does not return anything...

Yeah my done event just returns shop and null pair

kheekheekhee commented 3 years ago

You can solve this by having DoneEvent return another DoneEvent and in your Main you check if the current Event and the next Event are the same types of Events, if they're the same you don't add the next Event back into the priority queue. I did the same thing for LeaveEvent as well

rcdl2222 commented 3 years ago

My solution is similar to @taneshin , I moved the logic to Main and did the checking within Main instead of DoneEvent. Instead of null, I created a DummyEvent class to represent the chain of events "ending", as I was not sure if we could work with null (but the idea is the same). I used it for LeaveEvent too. And at the end of a loop, I will remove the DummyEvents in queue.

You can queue a ServeEvent immediately after DoneEvent (after satisfying certain conditions) in Main.