Optional factory variations
Like the Factory Method pattern, abstract factories have useful variations that you’ll see pop up in the programming wilds. The two we’ll talk about in the following subsections are the parameterized abstract and Concrete Factory, and the factory of factories (so meta).
Parameterized factories
Firstly, the parameterized Abstract Factory is almost identical to the parameterized Factory Method variation we built in the last chapter. The idea is to have the methods in any Abstract Factory class take a parameter value specifying which object to create and return. For example, we could update the recipe factory to take a string
name and use a switch
statement inside each method to return the correct object, as shown in the following code:
This isn’t the most scalable approach but adding reflection or generics can greatly increase how flexible the parameterized factory can be. Again, we covered this extensively in Chapter...