emilly
emilly

Reputation: 10530

Application of command design pattern in this use case?

Command pattern :- A request is wrapped under an object as command and passed to invoker object. Invoker object looks for the appropriate object which can handle this command and passes the command to the corresponding object which executes the command.

Consider a Employee management system where HR person can create/read/update/delete employee. In my career I have always designed this use case something like

Request from browser ->  EmployeeController -> EmployeeService -> EmployeeDao

EmployeeService.java will contain all create/read/update/delete operations which will be called from controller

After reading Command pattern , I see another way of doing it where controller will work as Invoker to call appropriate CommandHandler like DeleteCommandHandler/UpdateCommandHandler/CreateCommandHandler/ViewCommandHandler

Is applying Command design pattern makes sense here ?

Upvotes: 1

Views: 426

Answers (1)

Kedar Tokekar
Kedar Tokekar

Reputation: 418

Command design pattern will provide good layer of indirection and it need not be limited to only CRUD commands. May be service layer is more appropriate Invoker as Command object can be passed through within the layer for better usage. Command object is pre-programmed with Receiver object (in form of your handlers) which is capable of handling request. Added advantage will be auditing (command layer actions like logging) of commands as they are invoked independent of from where those were invoked.

Upvotes: 1

Related Questions