When I first laid eyes on Java 8’s Stream API and found out that it worked lazily (then found out what that meant), I was blown away and utterly baffled. It didn’t seem possible at all. Eventually, I was able to wrap my head around it, realizing it was done with my favorite design pattern: the Decorator Pattern!
In this post, I’m going to go through and write up a simplified implementation, only going through the
reduce() methods. We’ll also be ignoring the ability to parallelize the calls. Lastly, for the sake of readability, I’ll be ignoring
extends in the generics. Continue Reading
Opening disclaimer: this isn’t always a good idea. I’ll present the idea, along with some of the reasons why it’s a good idea, but then I’ll talk about some instances where it’s not so great.
As you may know, I’ve been dabbling in Python nearly as much as I’ve been working with Java. One thing that I’ve liked about Python as soon I found out about it is generators. They allow for lazy operations on collections, so you can pass iterators/generators around until you finally actually need the final result of the operations – without affecting the original collection (under most circumstances; but you’re not likely to affect it accidentally). Continue Reading
The Decorator Pattern
Ever since first learning the programming design patterns, the decorator pattern has been my favorite. It seemed such a novel idea to me, and so much more interesting than the others. Don’t get me wrong, most of the others blew my mind too, but none so much as the decorator pattern. To this day, it’s still one of my favorites.
(If you’re unfamiliar with design patterns, I highly recommend Head First Design Patterns. If you just want to learn about the decorator pattern, here is an excerpt of the decorator chapter from that book.)
Personally, I believe the decorator pattern is generally underutilized. There’s a couple probably reasons for this. For one, I don’t think it applies to all that many situations. Another thing, problems that can be solved with the decorator pattern are generally fairly difficult to spot. What makes the pattern so mind-blowing to me is the same reason it can be difficult to figure out where it’s needed, that reason being that it’s such an unusual idea. That is, it seems to be until you’re strongly acquainted with the principle of “composition over inheritance”. Continue Reading