Jumping around between multiple languages can help you notice some differences between idioms and best practices in different languages. One of the more interesting differences has to do with one function doing multiple things. Continue Reading
Java
Sometimes, you may hear about dependency injection done via a getter method, which subclasses override or mock frameworks fake for testing. It allows the class to have a set dependency that is actually hard coded, but can be “injected” if need be. Continue Reading
Quite a while back, I posted about how, despite the fact that you should prefer composition over inheritance, you can best design classes for inheritance. Now, I wish to give some examples of how you can take your code that uses inheritance and change it to use composition instead, which will often actually make your code more flexible.
The code will be in java, but the concepts can transferred to any language that is object-oriented. Some languages might have constructs that make parts of this easier, too (Kotlin’s Delegates).
One thing is important to remember, though: Even if you switch “completely” to composition, you will still have some inheritance. This inheritance will be from interfaces and interface-like classes only, though. It barely counts as inheritance, really, since all it’s doing is restricting itself to an API and not inheriting implementation details. Continue Reading
I love the decorator pattern. It’s the epitome of composition over inheritance and the greatness that can come from it. It also has potential for fluent APIs. In the following article, the writer presents a generic decorating method that I really like, for times when you may extend the list of decorators in the future and you don’t want to add additional methods for them.
Check it out!
http://blog.codefx.org/design/patterns/decorator-pattern-java-8/
It’s a fairly well known refactoring pattern to replace conditionals with polymorphism. If you’re not familiar with the pattern, you can check it out here. But that basic solution can start to break down once there are multiple fields in the class that conditional checks are based off of. We’ll look into some possible ideas on how to work with these possibilities.



