I know this is an old post, but I hope this helps you or others who come across it.
- What is the definition of a service class? The book I am learning from does not define them and I have had trouble finding a definition online.
Everyone's definition is either very specific or very general. I take the general definition of, "a service class separates your business logic from other classes that you want to be reusable throughout an app".
- Why would we want to use a separate service class when all the methods can be written in one class?
Using the below as an example. Say you're building a GUI. You'll have,
- View class - creates your visuals for your gui.
- Controller class - contains the methods and functions for interacting with the view. (passing objects to the view from the DAO and vice versa, but ideally the DAO should be replaced by a service class).
- DAO class - handles the actual CRUD operations in a database (ideally light weight and little if any business logic).
Suppose your app needs to return a record. Your controller calls the Dao, gets the record and sends it to the view. Simple, until you need to do more with the record as part of a process, like getUser(), isUserActive(), sendAccountDisabledEmail(), updateUser(), removeUserAssignedProjects()...
Where would you put that code?
You wouldn't put it in the view, because of separation of concerns, other classes couldn't use the methods, readability and just plain tight coupling making changes harder. You can put it in the controller, but other classes wouldn't be able to reference those functions, and again, readability and tight coupling. You could put it in your Dao, and it would allow other classes to make use of the Dao and those operations, but now anytime you need to change something internal to the Dao, you risk breaking the Dao, you will have a big class that may be too hard to read and what would you do if you want to reference another Dao? At this point the Dao is doing too much.
Ideally, you would create a "service" class that handles all business logic for a group of operations. The benefit is you now have a place to call other classes (DAO's including other service classes) and have them interact with each other. Chances are you would refactor anyway to using "service" classes as your app grows and becomes harder to refactor without breaking other classes.
I'm not saying everything needs a service or has to use a service, because sometimes you can just call the DAO directly for simple things, but it's just a lot easier to manage your app if you have a consistent class hierarchy with a service layer from the start (aka don't put business logic in your DAO or reference other DAO's in your DAO).
Should all service classes follow the structure of:
Instance variables.
Default and non-default constructors
Accessor and mutator methods
toString method
Equals method
Other help methods as required
Or can service classes serve as something other than as a template for
objects?
This is very broad and it really depends on what your trying to do. Services can be instantiated for an operation and discarded, or manage states through it's own methods or others classes. The key being, as long as your class is "orchestrating" and not actually playing any tunes, then I'd feel comfortable accepting it as a service class.