如何重构这种方法?(春天+ Java8)

达多尔

我正在尝试重构下面的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 
蒂亚戈·procacem

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

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;
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章