45

Is there any difference between naming a class "Handler", "Manager" or "Controller"? IE: PurchaseManager, PurchaseHandler, PurchaseController.

Do these suffices convey the same meaning or is there a clear difference between them?

If there isn't a language-agnostic answer, consider Java as the language.

2
  • 1
    More context would be helpful here. Many (many!) frameworks have pretty clear definitions of the distinctions. However, there's no universal definition behind these words. What context are you working in? What framework are you using? What language are you using?
    – S.Lott
    Jun 7, 2011 at 17:06

4 Answers 4

28

Usually a 'Controller' is the interface between a user interface component and a model (e.g. Purchase). Controllers should be thin classes, doing little more than mapping user interface events to model functions.

A 'Manager' is a code smell. The purchase should manage itself, or it could be managed by an owning class, like Vendor or Buyer.

A 'Handler' is usually a single function wrapped in an object. These are needed when programming in legacy languages without first-class functions.

7
  • 1
    While I too don't like the concept of a 'Manager' I think the rationale for not having it handle its own business logic is that the programmer wants to separate a bean/pojo/dto from the complex business operations. Jul 23, 2013 at 0:49
  • 1
    While I agree with you about Manager from a language agnostics point of view, I would like to add that a Manager can be correct term to use in some cases. For example it is ok to speak of managers from a protocol point of view for example. For SNMP you have a Manager "managing" a set of Agents. This is the correct term to use in this case. This does however justify spontaneous uses of "Manager".
    – patrik
    Mar 28, 2017 at 8:15
  • Is this the same for Unity? Jun 10, 2021 at 6:51
  • 1
    @OlivierPons Sorry, no idea. What's Unity? Is that a DI framework for .NET? I don't do Windows. Jun 17, 2021 at 1:08
  • 1
    Generally you want to follow the usual practices for the technology you are using to make it easier for others with experience in that technology to understand your code. Jun 19, 2021 at 8:39
14

While my answer is not based on definition, I view something labeled as a "Controller" as an exclusive implementation versus a "Handler" as inclusive. I would expect to see only one existence of a "PurchaseController" and multiple possible versions of a "PurchaseHandler".

I think another common name for Controller is "Broker" - which slightly alters the perspective on it.

1
  • Thank you for these definitions. You have put in a lot of knowledge in few lines.
    – acearch
    Feb 15, 2019 at 6:00
2

As a PHP developer I work with MVC frameworks.

there we have

  • Views - mostly to keep graphical design
  • Models - wrapper around database tables
  • Controllers - Classes where every function is called from a route and returns a http response, like view or redirect or error (404)
  • Brokers - classes to get all the calculation and logic. It is like a black box that takes what you have and gives you back exactly what you wanted. It also serves like a replacement for documentation.
  • Handlers - mostly for Exception Handling and Event handling
  • Services - wrappers around APIs
  • Requests - to describe some inputs, it also can handle validation, authorization, typehinting, and some basic data processing, like get a model by provided id
  • Middleware - to abstract out some common logic from the multiple routes, like authentication and authorization
  • Helpers - some tiny classes and functions used throughout the project, like MoneyHelper or ValidationHelper
  • Factory - some interfaces can have multiple implementations depending on some input parameters, so the factory would give you the right one

...

  • Managers - (almost forget about it) - it is like factories, but for things other than creating, for example CacheManager, EventManager, QueueManager... Or if you need to manage a broker PasswordBrokerManager. It is basically a wrapper but with a better name like PayPalRequestManager.

You would also have

  • collections
  • calculators
  • validators
  • processors
  • fetchers and pushers
2
  • Building on your Managers example, but in my experience a Manager is often asynchronous and/or orchestrates the flow of other logic. Often used for publish/subscribe architectures. They can also be stateful, whereas you probably wouldn't want a stateful service or broker
    – Mars
    Jul 6, 2022 at 7:31
  • Although in pub/sub, a manager may also be called a broker...
    – Mars
    Jul 6, 2022 at 7:34
0

The terms has to do with the hierarchy of the classes, and as such there is no right or wrong, as long as the hierarchy makes sense.

Often old school factory hierarchies are used, with

  • one director
  • few managers
  • many workers

Handlers will sometimes replace managers, if there is no director.

Brokers should imo not be used in hierarchies, but is instead part of a communication model (broker / subscriber)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.