How can I remove duplicated code?It's hard to me because I use different service class and model each code.public class AcrAccessAreaController extends BaseController {    @Autowired                                                                         private AcrAccessAreaService acrAccessAreaService;     @RequestMapping(/action)    public String action(ModelMap model, AcrAccessAreaModel acrAccessAreaModel) {        try {            if(acrAccessAreaModel.getTrStatus().equals(TrStatus.DELETE))            {                Map cateData = acrAccessAreaService.chkUseYnArea(acrAccessAreaModel);                int cnt = Integer.parseInt(((AcrAccessAreaModel)cateData.get(chkInfo)).getCnt());                if(cnt > 0)                {                    super.setResult(model,Result.E04);                    return /dummy;                 }            }            Map accData = acrAccessAreaService.action(acrAccessAreaModel);            model.put(accAreaInfo,accData.get(accAreaInfo));            super.setResult(model);        } catch(Exception e) {            super.setResult(model, new ApprovalException(e));        }        return /dummy;     }}public class AcrAssociationController extends BaseController {    @Autowired                                                                         private AcrAssociationService acrAssociationService;     @RequestMapping(/action)    public String action(ModelMap model, AcrAssociationModel acrAssociationModel) {        try {            if(acrAssociationModel.getTrStatus().equals(TrStatus.DELETE))            {                Map assoData = acrAssociationService.chkUseYn(acrAssociationModel);                int cnt = Integer.parseInt(((AcrAssociationModel)assoData.get(chkInfo)).getCnt());                if(cnt > 0)                {                    super.setResult(model,Result.E04);                    return /dummy;                 }            }            Map assoData = acrAssociationService.action(acrAssociationModel);            model.put(assoInfo,assoData.get(assoInfo));            super.setResult(model);        } catch(Exception e) {            super.setResult(model, new ApprovalException(e));        }        return /dummy;     }}public class AcrCategoryController extends BaseController {    @Autowired                                                                         private AcrCategoryServiceacrCategoryService;        @RequestMapping(/action)    public String action(ModelMap model, AcrCategoryModel acrCategoryModel) {        try {            if(acrCategoryModel.getTrStatus().equals(TrStatus.DELETE))            {                Map cateData = acrCategoryService.chkUseYn(acrCategoryModel);                int cnt = Integer.parseInt(((AcrCategoryModel)cateData.get(chkInfo)).getCnt());                if(cnt > 0)                {                    super.setResult(model,Result.E04);                    return /dummy;                 }            }            Map cateData = acrCategoryService.action(acrCategoryModel);            model.put(categoryInfo,cateData.get(categoryInfo));            super.setResult(model);        } catch(Exception e) {            super.setResult(model, new ApprovalException(e));        }        return /dummy;     }}
Refactoring duplicated code
java
I will give you an idea. You need to have a dummy class with the functionality that you want. As you haven't provided much details I will make the assumption that the all models are sibiling classes. Same assumption for other things.After you have the dummy class you can just sub-class them and give the infoVariable a new value and override the action method. In action method just call super with the appropriate variables. public class DummyController extends BaseController {    String infoVariable = info;     @RequestMapping(/action)    public String action(ModelMap model, ParentInterfaceOfModels myModel) {        try {            if(myModel.getTrStatus().equals(TrStatus.DELETE))            {                Map data = myService.chkUseYn(myModel);                int cnt = Integer.parseInt(((AcrCategoryModel)data.get(chkInfo)).getCnt());                if(cnt > 0)                {                    super.setResult(model,Result.E04);                    return /dummy;                 }            }            Map data = acrCategoryService.action(myModel);            model.put(infoVariable,data.get(infoVariable));            super.setResult(model);        } catch(Exception e) {            super.setResult(model, new ApprovalException(e));        }        return /dummy;     }}I have not made all changes necessary because many things are not clear. In the typecasting I have left the Model class as it is. You can simply get the class name at runtime to do the casting.A simple example for casting to String class at runtime is java.lang.String.getClass().cast(obj). But how will you get the name of the class as String in the sub-classes? I will leave that to you to find out.