-1
\$\begingroup\$

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"; 
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Mind adding which framework this is for? Anything else about the functionality? \$\endgroup\$ Commented Jun 8, 2014 at 4:19
  • \$\begingroup\$ For this condition, you can use multiActionController. \$\endgroup\$
    – user78304
    Commented Jul 20, 2015 at 9:41
  • \$\begingroup\$ The desire to improve code is implied for all questions on this site. Question titles should reflect the purpose of the code, not how you wish to have it reworked. See How to Ask. \$\endgroup\$
    – Jamal
    Commented Jul 20, 2015 at 15:06

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$
6
  • \$\begingroup\$ Oh, it's very useful for me. now i am going to try. i really appreciate for it. and could you check codereview.stackexchange.com/questions/52492/… in this article my last question~?? \$\endgroup\$ Commented Jun 8, 2014 at 5:01
  • \$\begingroup\$ @user1842715 I can take a look but before that at least tell what framework is this code for? What is the purpose of this one other than being part of a web application? \$\endgroup\$ Commented Jun 8, 2014 at 5:13
  • \$\begingroup\$ i am using Spring framework~ for web application having controller, service, dao and model. and i have question with above code, how i can handle myService?? do i have superclass for services? and using superclass?? \$\endgroup\$ Commented Jun 8, 2014 at 5:20
  • \$\begingroup\$ Only you can tell whether or not your services have a common superclass. I have not seen your complete code so I cannot tell you that. Is the code on github or any other open-source repository? As per the other question's code there is a superclass - BaseService if the code has not been changed. \$\endgroup\$ Commented Jun 8, 2014 at 5:33
  • \$\begingroup\$ yes, i have BaseService now. but i don't know well how apply with above code \$\endgroup\$ Commented Jun 8, 2014 at 5:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.