如何重构此方法?(spring和java8)

如何重构此方法?(spring和java8),spring,java-8,Spring,Java 8,我正在尝试重构下面的Java类。我有一个保存POJO(实体)的方法,它取决于它所属的实例 下面的代码仅显示3个服务,但总共有13个服务。 每个服务都在调用一个单独的*RepositoryImpl。 例如,ActiviteService是一个接口,ActiviteService.create(activity)将调用该接口的实现 @Autowired private ActiviteService activiteService; @Autowired private AdresseM

我正在尝试重构下面的Java类。我有一个保存POJO(实体)的方法,它取决于它所属的实例

下面的代码仅显示3个服务,但总共有13个服务。 每个服务都在调用一个单独的*RepositoryImpl。 例如,ActiviteService是一个接口,ActiviteService.create(activity)将调用该接口的实现

@Autowired
private ActiviteService       activiteService;
@Autowired
private AdresseMsSanteService    adresseMsSanteService;
@Autowired
private AttributionParticuliereService     attributionParticuliereService;



private boolean sauvegarder(Object object, Long idIdPlay, String game,
    Integer gameIndex) {


    boolean isSaved = false;

    if (idIdPlay == null) {
        throw new IllegalArgumentException("IdPlay id is null");
    }

    if (object instanceof Activite) {
        Activite activite = (Activite) object;
        activite.setIdIdPlay(idIdPlay);
        if (this.isGameOn(activite, game, gameIndex)) {
            activiteService.create(activite);
            isSaved = true;
        }
    } else if (object instanceof AdresseMsSante) {
        AdresseMsSante adresseMsSante = (AdresseMsSante) object;
        adresseMsSante.setIdIdPlay(idIdPlay);
        if (this.isGameOn(adresseMsSante, game, gameIndex)) {
            adresseMsSanteService.create(adresseMsSante);
            isSaved = true;
        }
    } else if (object instanceof AttributionParticuliere) {
        AttributionParticuliere attributionParticuliere = (AttributionParticuliere) object;
        attributionParticuliere.setIdIdPlay(idIdPlay);
        if (this.isGameOn(attributionParticuliere, game, gameIndex)) {
            attributionParticuliereService.create(attributionParticuliere);
            isSaved = true;
        }
    } else if 

首先,我将创建一个代表游戏实体的界面。例如:

public interface GameEntity {
    void setIdIdPlay(Long idIdPlay);
}
然后,创建实现GameEntity接口的类:

@Entity
@Table
public class AdresseMsSante implements GameEntity {
    @Id
    Long idIdPlay;

    public void setIdIdPlay(Long idIdPlay) {
        this.idIdPlay = idIdPlay;
    }
}


@Entity
@Table
public class Activite implements GameEntity {
    @Id
    Long idIdPlay;

    public void setIdIdPlay(Long idIdPlay) {
        this.idIdPlay = idIdPlay;
    }
}
然后,创建您的通用存储库,它将保存每个游戏实体

@Repository
public class Repo {
    @Autowired
    EntityManager entityManager;

    @Transactional
    public void save(GameEntity obj) {
        entityManager.merge(obj);
    }
}
最后,您的方法如下所示:

 @Autowired
    Repo repo;

 private boolean sauvegarder(Object object, Long idIdPlay, String game,
                                Integer gameIndex) {
        boolean isSaved = false;
        if (idIdPlay == null) {
            throw new IllegalArgumentException("IdPlay id is null");
        }
        GameEntity gameEntity = (GameEntity) object;
        gameEntity.setIdIdPlay(idIdPlay);
        if(this.isGameOn(gameEntity, game, gameIndex)) {
            repo.save(gameEntity);
            isSaved = true;
        }
        return isSaved;
    }


    boolean isGameOn(GameEntity gameEntity,  String game, Integer gameIndex) {
        // do something
        return true;
    }