[Reading Memo] Refactoring-Typecode replacement (for typecodes that affect behavior)-1

(What) What is the type code that affects the behavior?

For example, like this.

Type code that affects behavior.java


public int getPayAmount() {
    switch (getType()) {
        case ENGINEER:
            return getMonthlySalary();
        case SALESMAN:
            return getMonthlySalary() + getCommission();
        case MANAGER:
            return getMonthlySalary() + getBonus();
        default:
            return 0;
    }
}

This is where the type code is determined and different code is executed depending on the result. Generally, the ʻif --otherandswitch` statements correspond to the parts that are the subject of this time.

If you look at such a code, what you want to do is as follows.

  1. Replace the type code with an inheritance structure that achieves polymorphic behavior
  2. Replace conditional branching with polymorphism

(Why) Why replace it?

I am trying to replace the parts that are processing differently depending on the judgment result of the type code, but the advantage of this is ↓

★ You can remove knowledge about behavior under different conditions from the client.
    =>As a result, when adding a new variation, it becomes possible to respond only by adding the variation.
    =>Without polymorphism, you would have to find all the existing conditional branches and add processing to each.
        =>This suggests that additional leaks may occur and is a hotbed for failure.

(How) How to replace it?

Subclass replacement

Change before.java


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case REQUEST_CODE_FOR_EVALUATION_SEND:
                       //Processing something
            break;
        case REQUEST_CODE_FOR_SMS_SEND_ACTIVITY:
                       //Processing something
            break;
        case REQUEST_CODE_FOR_CAMERA_USAGE:
                       //Processing something
            break;
        default:
    }
}

After change.java


class ActivityResult {
    public static final int REQUEST_CODE_FOR_EVALUATION_SEND = 1;
    public static final int REQUEST_CODE_FOR_SMS_SEND_ACTIVITY = 2;
    public static final int REQUEST_CODE_FOR_CAMERA_USAGE = 3;

    static ActivityResult create(int requestCode) {
        switch (requestCode) {
            case REQUEST_CODE_FOR_EVALUATION_SEND:
                return new ActivityResultEvaluationSend();
            case REQUEST_CODE_FOR_SMS_SEND_ACTIVITY:
                return new ActivityResultSmsSend();
            .. //Same for other codes
        }
    }

    private ActivityResult() {}

    abstract int getRequestCode();
}

class ActivityResultEvaluationSend extends ActivityResult {
    @Override
    public int getRequestCode() {
        return ActivityResult.REQUEST_CODE_FOR_EVALUATION_SEND;
    }
}

class ActivityResultSmsSend extends ActivityResult {
    @Override
    public int getRequestCode() {
        return ActivityResult.REQUEST_CODE_FOR_SMS_SEND_ACTIVITY;
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    ActivityResult result = ActivityResult.create(requestCode);
    // @Implement the process in todo result.
}

You have now replaced it with a subclass. This isn't the final form of what I wanted to do, but since it became polymorphism after this, I would implement each process in each subclass. However, I will write it separately.

Recommended Posts

[Reading Memo] Refactoring-Typecode replacement (for typecodes that affect behavior)-2
[Reading Memo] Refactoring-Typecode replacement (for typecodes that affect behavior)-1
A memo for myself that object-oriented is something