Let us suppose we have an Order class and a method in which some services make a sequence of operations
fun doStuff(order: Order): Order {
val orderByServiceA = serviceA.operationA(order: Order)
val orderByServiceB = serviceB.operationB(orderByServiceA: Order)
val orderByServiceC = serviceC.operationC(orderByServiceB: Order)
return orderByServiceC
}
I create a common interface who can be implemented by the services
interface IOrderService {
fun operation(order: Order): Order
}
So the method above become
fun doStuff(order: Order): Order {
val orderByServiceA = serviceA.operation(order: Order)
val orderByServiceB = serviceB.operation(orderByServiceA: Order)
val orderByServiceC = serviceC.operation(orderByServiceB: Order)
return orderByServiceC
}
There's some kind of repetition in this code so, next step is to make this function open for extension and closed for modification. I add into a list all the instances of IOrderService, so the function changes like
fun doStuff(order: Order): Order {
orderServices.forEach { service ->
service.operation(order)
}
...
}
This solution is quite good because if I want to add another service who implement the operation function, it's very easy to do it.
But there is a problem.. the order object passed in the last solution it's the same for all the services so the solution proposed it's different from the first implementation. Every service need to do the operation function based on the previous result
So I'm quite stuck at this point, because I would like to find a clean solution.. I was thinking at some design pattern for example Observer (but it's a one-to-many solution and it's not the case) Memento pattern (but probably I would continue to have a sequential list of operations), publish subscribe... but I don't think any of those are the right choise. It's like a domino in which the object is passed to a first service, the result to the second one, etc. etc. and of course the final result returned by the doStuff method.
Any suggestions to complete the last piece of code? Thanks a lot!