Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What we should use instead of a "manager" class in a good OOP design?

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:

  • Load resources.
  • Map resources by an identifier.
  • Ensure that resources are loaded only once.
  • Free unused resources (using smartpointers).

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!

like image 703
Dani Avatar asked Nov 01 '10 11:11

Dani


People also ask

What should be a class in OOP?

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.

What does manager class do?

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.

What is a good OO design?

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.


1 Answers

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.

like image 157
Mark Seemann Avatar answered Oct 02 '22 10:10

Mark Seemann