I want to apply the strategy pattern in a microservice where your only responsibility is to take the received payload, validate this payload, adapt the payload and publish it to a database. But I have some variables for this case: I have a context, an origin and an action that will be received in the input payload, following this pattern:
origin: { food, non-food }
context: { cart, order, catalog }
actionCart: { getCart, addItemCart, removeItemCart }
actionOrder: { getOrder, createOrder, getHistory }
actionCatalog: { getCatalog }
data: payload
In short, depending on the origin I will have a context, depending on the context I will have an action, and depending on the action I will have the methods with their adapters and finally publish in the specific database for each call.
I know it's something complex and difficult to understand, so I made a code exemplifying with nested ifs what the code would look like without applying the strategy:
if (food) {
if (context1) {
// all actions publish to database context 1
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank }
if (action3) {
// assemble specific payload and publish it to the bank
}
}
if (context2) {
// all actions publish to database context 2
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank
}
if (action3) {
// assemble specific payload and publish it to the bank
}
}
if (contex3) {
// all actions publish to database 3
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank
}
if (action3) {
// assemble specific payload and publish it to the bank
}
}
}
if (non - food) {
if (context1) {
// all actions publish to database context 1
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank
}
if (action3) {
// assemble specific payload and publish it to the bank
}
}
if (context2) {
// all actions publish to database context 2
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank
}
if (action3) {
// assemble specific payload and publish it to the bank
}
}
if (context3) {
// all actions publish to database context 3
if (action1) {
// assemble specific payload and publish it to the bank
}
if (action2) {
// assemble specific payload and publish it to the bank
}
if (action3) {
// assemble specific payload and publish it to the bank
}
}
}
There is the possibility of applying a strategy for each, for example: strategy for the context and within the context method to have a strategy for the action, but I don't think it's a very good solution.
Can anyone help me with this?