0

I know this topic has 2 kinda of duplicates by there where TL;DR; use method override, mine i belive is not that simple.This will be basically a theoritical question i wont link the code cause its just to long.

Imagine that we have 3 different types of emergency centers which all inherit an abstract class which implements an interfaces (FireServiceCenter, MedicalServiceCenter and PoliceServiceCenter)

And 3 types of emergencies which all inherit an abstract class which implements an interface (OrderEmergency, PropertyEmergency, HealthEmergency)

Each of the 3 centers can only take care of 1 type of emergency and there are only 3 centers not more and all are different types, i.e. we can handle all emergencies.

FireServiceCenter - > PropertyEmergency

MedicalServiceCenter - > HealthEmergency

PoliceServiceCenter - >OrderEmergency

The input will be, let's say, 500 different emergencies and just link them to their respective center

How can I accomplish this without using instanceOf?

My first thought was to just add an extra final static field to each center and emergency class witch I would have called typeOfCenter/typeOfEmergency and add all of the emergencies in 1 collection and then all centers in 1 collection and then just match them together (with 2 for loops: 1 for centers and 1 of emergencies), but I don't know how this is any different frominstanceOf cause i will be using .equals for their fields.

Robo Mop
  • 3,485
  • 1
  • 10
  • 23
Все Едно
  • 746
  • 3
  • 10
  • 24
  • 3
    Sounds like you need [multi-dispatching](https://stackoverflow.com/questions/29458676/how-to-avoid-instanceof-when-implementing-factory-design-pattern/29459571#29459571) – Vince Jun 01 '17 at 19:00
  • 1
    Pass the emergency an object which holds the various centers (Say, with a getter method for each); have the emergency pick the center it wants to deal with. (This is double dispatch) – Andy Turner Jun 01 '17 at 19:09
  • Vince Emigh ok thanks it seems this topic you linked hold the asnwer i looked of, but of course my habit of not reading more then 5 seconds got the best of me again, i actualyl check that topic before i posted my own, if you want post an asnwer i will mark it as best. I will delete the topc in 10 min or so. – Все Едно Jun 01 '17 at 19:26
  • 1
    @ВсеЕдно It's best to mark this post as duplicate. I'm not a huge fan of rep hounding. If you need help implementing, feel free to start a chat and invite me :) – Vince Jun 01 '17 at 19:53

0 Answers0