Java patterns revisited - Factory
Everybody at least heard about Design Patterns. With the advent of Java 8 some design patterns in Java can be done differently than till now. Actually since there is Dependency Injection some additional patterns can be rewritten to be oh so pretty.
I thought why the hell not. So here it comes the first one, Factory.
Every body nows and used factory pattern even if they didn’t know about it. So how did it look till now ?
It looked like this
or this
Where Base is some interface and returned objects are it’s implementations.
So how does it change with Java 8 ? It becomes just like factory in c++. Well, almost. The idea is the same Map with methods returning proper object. In c++ value of map is reference to function/method, in java it’s just Supplier anonymous implementation. In my opinion it’s even prettier than in c++, because you see everything and don’t need to look for function that reference is pointing to.
That’s all folks. Next time will probably be some pattern rewritten with DI.