vendredi 11 novembre 2016

how to write these code maintainable and flexible

i have interface

 public interface IService{
    public Map<String, Object> statistics(Map<String,Object> parameters);
}

and CommonService extract common method

public class CommonService {
         public Document statisticsSex(List<Long> userIds, List<Long> resultIds) {
            .......
     }
 public Document statisticsUserType(List<Long> userIds, List<Long> resultIds) {
            .......
     }
 public Document statisticsDepts(List<Long> userIds, List<Long> resultIds) {
            .......
     }
    }

and have some special service

 class SpecialService extend CommonService implement IService{
     public Map<String, Object> statistics(Map<String,Object> parameters){
        statisticsSex();
        statisticsUserType();
        statisticsDepts();
        //invoke some special methods
    }
  }  

i think the program can be better. why not use composite instead inherit? so i change to

    class SpecialService  implement IService{
             private CommonService commonService;
             public SpecialService(){
                 commonService = new CommonService();
            }
             public Map<String, Object> statistics(Map<String,Object> parameters){
                statisticsSex();
                statisticsUserType();
                statisticsDepts();
                //invoke some special methods
            }
  public Document statisticsSex(List<Long> userIds, List<Long> resultIds) {
                commonService.statisticsSex(userIds,resultIds);
         }
     public Document statisticsUserType(List<Long> userIds, List<Long> resultIds) {
                commonService.statisticsUserType(userIds,resultIds);
         }
     public Document statisticsDepts(List<Long> userIds, List<Long> resultIds) {
                commonService.statisticsDepts(userIds,resultIds);
         }
          }  

but the class have to implement these method again,it is repeated. then i change another way. i deleted the CommonService class and IService,create a CollectiveReportService class.

public abstract class CollectiveReportService {
protected final Document statisticsSex(List<Long> userIds, List<Long> resultIds) {
  ......
}
protected final Document statisticsUserType(List<Long> userIds, List<Long> resultIds) {
  ......
}
protected final Document statisticsDepts(List<Long> userIds, List<Long> resultIds) {
  ......
}
}

and other inherit it. i try to convince my co-worker to follow my way,but he say the CommonService is just tool class which extract some common method,you can inherit it or not,my way is not flexible.

how to write these code maintainable and flexible?

Aucun commentaire:

Enregistrer un commentaire