While reading Effective Java Item 21: Use function objects to represent strategies, something hit my mind and now I’m writing it down. All these new lambda thing is really what is called the Strategy pattern in the OOP world, or would it be more appropriate to say that the Strategy Pattern in Design patterns is really what lambda is. The essence of all these, can be boiled down to one simple idea, pass functionalities around.
Object Oriented Design
I have been working on some Clojure project at work for several months now, one little thing bothers me now and then is that it doesn’t allow dependency cycle in project. For example if a.clj requires b.clj, b.clj requires c.clj, then c.clj cannot require a.clj, in other words, c.clj cannot use anything inside a.clj. At first, I thought this is a bit odd, java doesn’t have that. And once in a while we have to solve such problems by creating a new namespace and move the common function into it.
One of uncle bob’s videos talks about how to design a coffee maker, I think he nailed it. The problem is to implement a software component that controls a coffee maker. Requirement The Mark IV Special makes up to 12 cups of coffee at a time. The user places a filter in the filter holder, fills the filter with coffee grounds, and slides the filter holder into its receptacle.
Recently at work we has been talking about implementing some kind of Analytic interface for all the analytic libraries we are using, like Localytics and Firebase and so on. Basically it is just a fat interface with a long list of event logging functions, like logSignIn(), logSignOut(), logSellProduct(), logOpenMap() and so on. There are about 40 such methods in that interface. So this is how we implemented it in the first place.
One day, I was watching another Uncle Bob’s video (yes, they are addictive), when I see one example he gave when talking about open closed principle, it ringed a bell in my head. This looked familiar! The type in some data classes, some switchs or ifs, some &&s and ||s all dancing around in the class. I can almost hear them teasing: “Come and catch me! Come and catch me!”.