I'm having trouble designing my game engine. For example:
When I think in Resources, I think in a ResourceManager class to manage resources on my engine.
This class gets several responsibilities:
This works for me, but I have read on every OOP design tutorial that managers are ambiguous classes with high coupling and low cohesion. I understand this, and I agree, but I searched almost the whole Internet to find a clear example of a real alternative to Managers but I didn't found it.
Some people explain, for example that a ResourceManager should be divided into smaller classes: A ResourceFactory, a ResourceCollection, a ResourceCache, a ResourceFacade...
I know all this design patterns (Factory, Collection, Facade, etc.) But I don't actually understand how this can be joined to create a (easy to manage) resource management system.
My question is: Is there some tutorial or document with a clear example? Can someone explain an example of this system? I'll thank you if you can write a small example in C++ or another similar language.
Thanks in advance for your help!
In object-oriented programming, a class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods). The user-defined objects are created using the class keyword.
The Manager class will consult its list of DBConnection objects, and determine if any of them is un-allocated, and return one. If all are allocated, it will either create one and add to the pool (subject to max connections allowed limit) or place the request on queue, or report back failure.
Gamma's second principle of good OO design is to: Favor object composition over class inheritance. Systems that follow this rule have fewer classes and more objects. Their power is in the ways that the objects can interact with each other. It would be important to have good dynamic models.
You almost said it already:
This class gets several responsibilities
(original emphasis).
This violates the Single Responsibility Principle. Additionally, the word Manager suggest an object that manages other objects, which is at odds with the object-oriented principle that objects should encapsulate both data and behavior. This quickly leads to the Feature Envy code smell.
Dividing the manager into many smaller classes is the correct thing to do. To keep them manageable, you can implement a Facade over them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With