Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove duplication

Tags:

java

I have a class contains 10 methods which are doing almost the same things apart from one key event. Two examples are given below:


Public String ATypeOperation(String pin, String amount){
    doSomething();
    doMoreStuff();
    requestBuilder.buildATypeRequest(pin, amount);
    doAfterStuff();
}



Public String BTypeOperation(String name, String sex, String age){
    doSomething();
    doMoreStuff();
    requestBuilder.buildBTypeRequest(name, sex, age);
    doAfterStuff();
}

As you can see from the above methods, they are similar apart from calling different methods provided by requestBuilder. The rest 8 are similar too. There is a lot duplicated code here. I feel there is a better way to implement this, but don’t know how. Any ideas and suggestions are appreciated.

Thanks, Sarah

like image 511
sarahTheButterFly Avatar asked Aug 03 '10 07:08

sarahTheButterFly


People also ask

Is there a quick way to delete duplicates in Excel?

To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.

What is the formula to remove duplicates in Excel?

To begin with, select the range in which you want to ddelete dupes. To select the entire table, press Ctrl + A. Go to the Data tab > Data Tools group, and click the Remove Duplicates button. The Remove Duplicates dialog box will open, you select the columns to check for duplicates, and click OK.


1 Answers

Use something like RequestBuilder, that accepts all these kinds of parameters:

public RequestBuilder {
    // setters and getters for all properties

    public Request build() {
         doStuff();
         Request request = new Request(this);
         doAfterStuff();
         return request;
    }
}

and then

new RequestBuilder().setAge(age).setName(name).build();
like image 138
Bozho Avatar answered Sep 27 '22 21:09

Bozho