what are (and the difference between) factories, s

2019-05-12 18:35发布

问题:

I am trying to get my head around the concepts of a typical software project, but I cannot find a decent article concerning this.

  • What are factories?
  • What are services?
  • What are util classes?

  • And what are the key differences between them? (ex, what makes a factory not a service, etc)

  • And are there any more concepts in a software project that are common?

回答1:

A factory is an implementation of The Factory Pattern. It's a way of creating objects.

A service is class that manages the concerns of a particular aspect of you application. For example, if you application operated on People objects, you might have some sort of PersonService to manage all the concerns of creating, editing, deleting, etc. Multiple services can be used by other services to accomplish goals that touch upon more than one domain area of your app. For example, if a Person can be added to a Company, the PersonService and CompanyService can work together to do the necessary operations. Usually there would be another service like YourAppService that coordinates those two services. When you do this it is called The Facade Pattern.

A utility class is a class that holds operations that are general to your application. For example, say you need to use some regexes all over you app. You could have a TextUtil that operates on Strings and whatnot anywhere. In Java projects, I usually create a TestUtil class that has methods to create objects that I will use in unit tests (the test util is itself tested, too). You could consider this use case an implementation of the factory pattern.

Some people think that if you have utility classes that you use in your application, it is a symptom of bad design.