How do I design a Service Layer in code?

I am currently rewriting one of my projects in Java and want to implement a service layer that will be used by different clients (CLI/GUI/Web) to communicate with my core application. For now it will mostly just delegate the same calls to the Business Model, but I want to add User authentication later on and I read, that a service layer is a good hook to do so.

But I don’t know how to write a service layer in code, so that it’s easily reusable with the rest of my code.

Here is what I could think of:

  • One Monolithic Service class that encapsulates all functionality of the application (Easy to inject into the client code, since it’s one class that can be used to do everything)
  • Multiple Service classes, separating different functionality from each other (Better encapsulation, but more injection code)
  • Multiple Service classes with a Service Provider class that knows all the other service classes (Basically option 2, but easier to inject since the Service Provider can be used to access the whole Service Layer)
  • Static Service classes (Easy to access, but has all the drawbacks of static classes/methods)

Option 2 probably is the way to go, but hearing the term Service Layer, I somehow think of this big class providing me with everything I ever wanted to do with my application.

What is the correct way to implement a Service Layer?


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *