Understanding a Few Creational Patterns
Abstract Factory is about creating a whole family of products in a unified way. That is, it’s like a template: one concrete factory creates a series of products, and that series probably shares a single style, so different product series get assigned to different factories. The downside is that the product lineup for the series is already known in advance; if you want to add a new product to the series, you have to extend the factory and modify the low-level code. This pattern can be used to control variation — in other words, if I want to make a special modification to one product within an otherwise uniform series, I have to generate a whole new factory.
Builder, I personally feel, is very similar to Abstract Factory. The reason it’s called Builder is that the interface it provides doesn’t produce the basic parts (that’s what Abstract Factory does) — after producing the parts it also takes responsibility for assembling and combining them (Abstract Factory doesn’t do the combining). That way the caller doesn’t have to care which parts make up the final product; it just needs to find a Builder that can produce the final product. And by deriving from Builder, you can get parts of different specifications combined in different ways. Put plainly, Builder has one extra assembly step compared with Abstract Factory. If the assembly process and the details of the parts can’t be determined in the low-level code, use Builder; conversely, if the assembly process and the part specifications must be constrained down at the low level, use Abstract Factory.
Factory Method is a bit similar to Abstract Factory, but Abstract Factory supplies a whole set of parts while Factory Method supplies a single part. Once a product’s assembly method and part specifications are fixed, you can use Factory Method and let derived classes create the parts they need according to those specifications.
I bought the Design Patterns book in 2002. Honestly, I couldn’t understand a word of it back then — I flipped through a few pages, got a headache and put it aside. After doing this work for so long, I dug it out again and found I could understand a bit of it. Take these creational patterns: mapped onto real industrial production, they’re like an assembly line, and the question is under what circumstances you can make that line produce more types of products. The line takes in parts with specifications and puts out a final product. For example, Abstract Factory exists to supply parts to this line, and each concrete factory supplies a whole set of parts — but their specifications match, so they work with this line. If a part with a specification that doesn’t fit turns up, the line simply can’t run.
Factory Method is the kind of line where every step and every part specification is fixed. If you want to change the line, you only need to swap parts — you decide which part to swap, as long as it’s within the part’s specification range (that’s overriding the factory method).
Builder is a self-sufficient line. The person using it doesn’t need to know how it works or which parts it needs — they only need to know whether the final product it makes is the one they want. That way the line’s provider can redefine the parts and the assembly method.