mardi 18 mai 2021

Inheritance versus beans of the same service with different parameters (JAVA)?

I have a Spring Boot service class, let's call it BossService. BossService has one method, let's call it handle(). The problem is, I need three instances of BossService with different, autowired fields and can't decide between these approaches -

a) example of the first approach :

public class BossService{
Object1 a;
Object2 b;
Object3 c;

BossService(Object1 a, Object2 b, Object3 c){...};

void handle(){...};

}

@Configuration
public class Configuration(){

@Bean
public BossService bossServiceA(Object1 a, Object2 b, Object3 c){
return new BossService(a, b, c);
}

@Bean
public BossService bossServiceB(Object1 aa, Object2 bb, Object3 cc){
return new BossService(aa, bb, cc);
}

@Bean
public BossService bossServiceC(Object1 aaa, Object2 bbb, Object3 ccc){
return new BossService(aaa, bbb, ccc);
}

}

b) the second approach:

public abstract class BossService{
Object1 a;
Object2 b;
Object3 c;

BossService(Object1 a, Object2 b, Object3 c){...};

void handle(){...};

}

@Service
public class BossServiceA extends BossService{
BossServiceA(Object1 a, Object2 b, Object3 c){
super(a, b, c);
}
}

@Service
public class BossServiceB extends BossService{
BossServiceB(Object1 aa, Object2 bb, Object3 cc){
super(aa, bb, cc);
}
}

@Service
public class BossServiceC extends BossService{
BossServiceC(Object1 aaa, Object2 bbb, Object3 ccc){
super(aaa, bbb, ccc);
}
}

Aucun commentaire:

Enregistrer un commentaire