Controller GRASP Pattern

The Controller is responsible for handling the requests of actors. The Controller is the middle-man between your user clicking “Send” and your back-end making that happen.

Key Points

  • When a request comes from the UI layer object, Controller pattern helps us in determining what is that first object that receives the message from the UI layer objects.
  • This object is called a controller object which receives a request from the UI layer object and then controls/coordinates with other objects of the domain layer to fulfill the request.
  • It delegates the work to other class and coordinates the overall activity

Problem

Who should be responsible for handling an input system event? An input system event is an event generated by an external actor. They are associated with system operations of the system in response to system events, just as messages and methods are related.
For example, when a writer using a word processor presses the "spell check" button, he is generating a system event indicating "perform a spell check."
A Controller is a non-user interface object responsible for receiving or handling a system event. A Controller defines the method for the system operation.
Who should be responsible for handling an input event, which object/s beyond the UI layer receives interaction?

Solution

Assign the responsibility for receiving or handling a system event message to a class representing one of the following choices:
  • Represents the overall system, device, or subsystem (facade controller).
  • Represents a use case scenario within which the system event occurs, often named Handler, Coordinator, or Session (use-case or session controller).
  • Use the same controller class for all system events in the same use case scenario.

Example for Controller


Benefits

  • Either the UI classes or the problem/software domain classes can change without affecting the other side.
  • The controller is a simple class that mediates between the UI and problem domain classes, just forwards
  • event handling requests
  • output requests
The Controller is also an important idiom in modern web development frameworks, in forming a pillar of the Model-View-Controller architectural pattern. Controllers are used in AngularJS, Spring MVC, Struts MVC, Ruby on Rails, Sails and more.

Comments