Posts

Showing posts from October, 2020
Using the builder pattern with subclasses [Back when I worked for Sun Microsystems, I had an occasional blog. It survived the Great Absorption into Oracle for a while, but it now seems to have gone the way of all things. Here's an entry from it, fished out of the Internet Archive.] Josh Bloch's  Effective Java  popularized the  Builder Pattern  as a more palatable way of constructing objects than constructors or factory methods when there are potentially many constructor parameters. The formulation in Effective Java makes for a particularly readable construction, like this: new Rectangle.Builder().height(250).width(300).color(PINK).build(); The advantage over a constructor invocation like  new Rectangle(250, 300, PINK);  here is that you don't have to guess whether 250 is the height or the width. More generally, if the constructor allowed you to specify many other parameters — such as position, opacity, transforms, effects, and so on — it would quickly get very messy. The b