Object-Oriented Design – Handling Large Class Responsibilities

designdesign-patternsobject-oriented

I applied principles of the GRASP and ended up having a class called Environment.
This class's responsibilities are to:

  • Keep information about services in the environment,i.e. environment definition (Service is another class)
  • Start/stop services meeting some criteria
  • Apply configuration changes to different service and keep list of updated services
  • Restart services with configuration change
  • Revert configuration changes at the end of session
  • Search/Give Service class based on criteria (name etc.)

According to OOD, this is not a problem: very cohesive responsibility is assigned to this class.
But on the other hand it's a problem since the class is too big, even though all responsibility assigned to it makes sense. If I want to divide this responsibility to between separate classes, then all these classes need to know about "environment definition", which makes coupling worse and those classes will have "feature envy".

What design patterns are applicable for such situation? Or what other principals can be applied to have cohesive, less coupled classes?

Thanks in advance.

Best Answer

You probably meant to have Environment as a Facade (facade pattern) and not to implement every detail in it.

Break out all functionality into other classes. Imho each of the responsibilities below should be handled by a separate class that the Environment class uses internally.

  1. Keep information about services in the environment,i.e. environment definition (Service is another class)
  2. Start/stop services meeting some criteria
  3. Apply configuration changes to different service and keep list of updated services
  4. Restart services with configuration change
  5. Revert configuration changes at the end of session
  6. Search/Give Service class based on criteria (name etc.)

Those should give you six new classes. It will probably lead to some other classes too so that you for instance can manage configuration from different classes.

Related Topic