Service Object
Intent
Create an object that provides services related to one or more objects or subsystems that abstracts processes away into a minimum amount of simpler calls to accomplish a goal.
Why
Although we would like to believe we can create a complete object-oriented business application without anything that smells of procedural code, we often cannot. When client code knows that a process needs to happen but not how or who is involved in the process, we benefit from a service that can provide access to the process and hide the manipulation of objects behind the scenes to get the job done. A service can act as a facade in front of the details of the business layer.
Implementation Notes
Service is a broad term, and in truth there are many different kinds of services. Technically any static method on an object is a service. Service methods are generally black boxes that take input and spit output.
A service should never act like a god object. It knows the process, but the brains of the operation always remain in the business objects it manipulates.
Types of services
- Subsystem Service / Application Service
- Create an object related to a subsystem that provides one or more services that provide a façade around the details of working with various objects to accomplish a task.
- See All About Application Services
- Provides an abstract interface to more concrete and complex details. For example, a call might be made to a transfer service that transfers money from one account to another. The Transfer Service simply has a method called Transfer. However, behind the scenes it logs that the transfer was requested, checks security rights, and moves the money around, etc.
- Manager / Director
- A manager is a service that works more like a wizard backend that directs client code through a multi-step process. A Login Director might provide sequential steps for logging someone into the system, updating the security log as it goes.
- A manager or director may often look like a regular business object in that it might have state. It might also be mutable so it can track what the client has done. A Test Manager might allow client code to step through and answer questions. See more information on Managers here.
- Factory
- Factories provide services like creation, data translation, and validation for a related business object. See more on Factories here.
- Library
- A library is a collection of simple functions related to a subject that can often be reused in multiple applications. For example, over time you might create your own library of functions for string manipulation for functions not provided by the language you are using.
- External Wrapper
- External wrappers are used to provide a useful interface to an external system. For example, a developer might write her own wrapper around functions that can work with Excel files, send emails, or get book information form Amazon’s web services.
- User Interface Controller
- A UI Controller provides specific functions required by a user interface in order to get work accomplished with objects that are often on a remote server. See UI Controller for more info.
Consequences
A process as understood by business analysts can be modeled in the system
Facades for complex processes can be constructed to shield client code from OO intricacies.
Facades for code that gets the job done but is not pretty can be constructed. Refactoring to a cleaner solution can be implemented behind the service without affecting the client.
Obvious step-by-step instructions can be modeled directly into code in the case of a manager.
Click here to view list of all Simple Object Patterns