samedi 3 septembre 2016

Always provide interface while autowiring?

I'm building a Spring application in which every entity(Person) which can perform 3 tasks, say, Walk, Talk & Think.

Since all of them are some sort of Person's capabilities, I'm thinking of making an interface Capability.java which will have a method performTask().

Above 3 classes will implement Capability.java & will also have their own set of other specific methods.

Since, these 3 classes are verbs, I'm making them as @Service, i.e.

  1. WalkServiceImpl.java
  2. TalkServiceImpl.java
  3. ThinkServiceImpl.java

Also, I'm thinking of making one @Controller - PersonController, which will call performTaks() of these 3 classes.

To use these classes in PersoneController, I'll have to autowire them. And as a general practice we should always @Autowire the interface.

But here the 3 classes have a common interface, with which I'll not be able to call performTaks() of all 3 services in the controller.

So, my question is:

  1. Should I additionally make 3 separate interfaces, viz. WalkService, TalkService & ThinkService, which extends Capability.java and autowire these classes in controller, or
  2. Directly autowire WalkServiceImpl, TalkServiceImpl & ThinkServiceImpl in controller?

If there is altogether better approch for this, then please suggest that well.

Thank You

Aucun commentaire:

Enregistrer un commentaire