Skip to content
Advertisement

How to design a resource association problem in Java

I have the following modeling problem:

as an exercise, I need to model a system that allocates units (police cars/fire trucks) to a specific emergency (fire/robbery/...). Therefore an emergency needs to say how many units it needs (for instance 2 fire trucks and 3 ambulances). Of course, these units need to be assigned to that emergency. I’ve modeled this as two abstract classes: Emergency and Unit with the real units and emergencies that inherit these classes.

Is there an elegant design so the emergency can tell in a generic way which units it needs (additional types of units and emergencies are expected)?

The programming language is Java.

I know this is probably one of the standard design patterns, but I’m quite new to these design patterns.

Advertisement

Answer

You have a lot of options with this. Some of the patterns that come to mind are Observer, Chain of Responsibility, and Visitor. However, a lot of non-pattern solutions seem reasonable too. Don’t be hypnotized into using patterns all the time.

That being said, a combination of patterns will probably be the best solution. Create a higher level class, some sort of a Director. Allow the Director to observe the Emergencies, using the Observer pattern. When the Director receives a work request, it goes to an Object Pool and modifies the Emergency as it sees fit.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement