Wanting to refactor this OO looking code that I just came across. author of the code says MySave class was created solely as a placeholder for keys
and inserts
maps.
client of MySave class always creates new instance of MySave per every request and use add() method to populate keys and inserts maps.
Then another function will use MySave()'s keys
and inserts
vals to retrieve those values from corresponding Maps and then send them to persistent layer etc.
Even though there are already flaws even in this OO design (e.g. though current usage is creating MySave object for every new request, following design doesn't prevent from creating only a single class instance and crating threadsafety issues with shared state, and it's leaking shared state via public vals
etc). Instead of fixing OO design I would want to convert this into a functional code.
What would your approach be in converting to FP paradigms? I am just starting to learn about Scalaz state monad but does that sound like a good FP alternative to this OO code, or anything more trivial than that can work?
class MySave {
val keys = mutable.Map[Entity Int]()
val inserts = mutable.Map[Entity, String]()
def add(d: Entity, value: String): \/[Throwable,Unit] = {
//this is a side effecting method which either updates `keys` or `inserts` map
// by simply reassigning `keys` or `inserts` vars e.g. keys += d ->value
}
}