S
S
ScRsa2016-10-28 09:47:12
Java
ScRsa, 2016-10-28 09:47:12

Where to take out the specific logic of the service?

The crux of the matter is this.
There is a service @Servicefor working with documents (creation, deletion, ...). The problem is that some operations may have specific features depending on the type of document. For example, when deleting one document, you need to clean up some of its additional data, when deleting another, check whether it can be deleted, when deleting a third, copy it to storage, etc. The service itself performs general logic (logging, general checks, actual deletion), but where should the document-specific logic be placed?
Inherit a specific service from a common one and override methods? But then you will have to partially duplicate the common code. Or inject a delegate method into the general delete method, which temporarily transfers control to specific code, and then returns to the main one? Or what's right? something I'm confused)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sirs, 2016-10-28
@sirs

Try using the strategy patter .
Create an enum listing all types of documents and write a handler for each type, the handler contains the save, read, edit, etc. methods. for each document type. You also make a default implementation if you do not need to change the logic for some type of document. In your shared service, you will call the handler for the document type at runtime and execute the necessary methods.

O
OnYourLips, 2016-10-28
@OnYourLips

You make handler classes (not services) of a certain interface, register them through DI in the service, and then, depending on the type of document, use the one you need in the service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question