mardi 16 février 2021

Which OOP design pattern to use for different actions on an object based on its field values?

I have entity in database, say, MonthPlan:

class MonthPlan {
    private boolean approved;

    // other fields
}

There is also REST interface, which accepts external requests based on which program changes entity instances. For example, request

class EditMonthPlanRequest {
    private long amount;

    // other fields
}

is used to change month plan amount.

What I need is to execute different actions on MonthPlan entity based on value of approved field. For example, code for mentioned request could be as following

MonthPlan plan = getPlan(...);
if (plan.isApproved()) {
    // actions using data from EditMonthPlanRequest
} else {
    // other actions using data from EditMonthPlanRequest
}

There would be 5-6 different requests each with exactly two variants of actions based on value of approved field of edited entity. What OOP design pattern can I use for such use case to write more concise code?

Aucun commentaire:

Enregistrer un commentaire