Remove duplication

I have a class containing 10 methods that do almost the same thing except for one key event. The following are two examples:


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, except for calling the various methods provided by requestBuilder. The remaining 8 are also similar. There is a lot of duplicate code here. I feel that there is a better way to implement this, but I don’t know how to do it. Any ideas or suggestions are welcome.

Thanks Sarah

+5
source share
6 answers

Use something like RequestBuilderthat takes all of these 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();
+5
source
interface RequestBuilder {
  void doStuff(params);
}

public RequestBuilder getARequestBuilder() {
  return new RequestBuilder() {
    void doStuff(params) {  
      // impl.details
    }
  }
}    

public RequestBuilder getBRequestBuilder() {
  return new RequestBuilder() {
    void doStuff(params) {  
      // impl.details
    }
  }
}    

public String buildRequest(yourParams, RequestBuilder builder){
  doBefore();
  builder.doStuff(yourParams);
  doAfter();
}

, Strategy. Command, , :)

Bozho Builder.

Head First Patterns. .

+2

- buildRequest. , , . , , : D (Extraneon , )

    // call somewhere in the code:
    Builder b = new BTypeBuilder();
    b.age = "20"; b.sex = "female"; b.name = "eve";
    String res = buildRequest(b);

    Public String buildRequest(Builder builder)
    {
        doSomething();
        doMoreStuff();
        builder.build();
        doAfterStuff();
    }

    // Command pattern
    class BTypeBuilder implements Builder
    {
        String name, age, sex;

        // Constructor here

        void build() 
        {
            // Do your stuff here
        }
    }

    class ATypeBuilder implements Builder
    {
        String pin, amount;

        // Constructor here

        void build() 
        {
            // Do your stuff here
        }
    }

    public interface Builder 
    { 
        void build();
    }
+1

In addition to other answers, this may also be useful for you (if you just want to hook up your method without using your parameters for the earlier and after methods)

interface Function0<R> {
    R apply();
}

public void performOperation(Function0<Void> operation) {
    doSomething(); 
    doBeforeStuff(); 
    operation.apply();
    doAfterStuff(); 

}

then you can use it like this:

    final RequestBuilder builder = new RequestBuilder();
    performOperation(new Function0<Void>() {
        public Void apply() {
            builder.buildATypeRequest("1234", "2445");
            return null;
        }
    });

    performOperation(new Function0<Void>() {
        public Void apply() {
            builder.buildBTypeRequest("1234", "2445", "1234");
            return null;
        }
    });
0
source

Instead of sending a long list of parameters, just click all the parameters on the map and send that map as an argument.

0
source

All Articles