javasoftware-designgrasp

Java design for Resources with ResourceTypes


I'm working on a task managing application which uses a ResourceManager to manage all the reservations of different Resources, each with their ResourceType. At the moment, there are 5 kinds of ResourceTypes and these will not change regularly, but it can occur.

My question is what would be the best way to approach the design for this in terms of OO design, GRASP principles and the overall maintainability of the code:

It seems to me that with option B, there'd be a higher cohesion concerning resources and their types, but I'm looking for certain (refactoring) scenarios in which one option will come out better than the other?


Solution

  • This would allow types to be represented as classes and added at runtime.enter image description here

    You could also have a ResourceFactory which decides which resource to make at runtime. Then you could use the factory to get the resource object to pass to addResource().

    The benefit of this design is ResourceManager is decoupled from the specific resource types it manages. To add a new resource type, just add a new class and update the factory class.