https://kotlinlang.org logo
#getting-started
Title
# getting-started
j

Juan B

10/30/2020, 4:51 AM
Hi all, I have a question about what would be a good way to implement a pattern where you have different components with responsibilities but the outcome of some may be an input of another component. I'm asking from the mutability/arguments perspective. E.g imagine a Chain of responsibility-ish (CoR) pattern where an object "Result" goes through multiple filters, each one generate an outcome based on some logic. This Result object is a complex object composed by some collections, other objects. Each Filter can add new pieces of information to the Result; and the order is important and can change. I'm wondering what would be a clean way to implement this in Kotlin. (Note that the computation is not stopped after the first processor handles the computation, it is reason I said CoR-ish) Some options I thought: • Common interface for filters with a Result class as an argument. Result as data class passed as argument in the call to each filter, inside I could clone and add new items but Clone is not deep so it wouldn't work with collections and more complex objects. • Do not use a data class but a normal class. I would have to implement getter and setter. I guess I could implement my own clone or use memory serialization/deserialization to force a deep clone (really don't want to do it) • Define specific Result Object per Filter so argument is inmutable. The main issue here is the order of execution can not be altered (it breaks the flexibility on the chain of responsibility pattern) Any suggestion or different approach? . Most of the examples I found for Single Responsibility in Kotlin does not cover the fact that the outcome from one filter may be needed in the next.
Any suggestion? 🤞