5 Building a crafting system with the Abstract Factory pattern
This chapter covers
- Pros and cons of the Abstract Factory pattern
- Working with families of related but independent products
- Working with families of related and dependent products
- Exploring different Abstract Factory variations
In the last chapter, we built an item production system using the Factory Method design pattern, Unity GameObjects, and reflection. In this chapter, we’ll focus on the Abstract Factory pattern for creating factories OF factories. While the Factory Method helped us hide the creation process of a single product or type of product, Abstract Factory lets us black box the creation process behind families, or related groups, of products. As we noted in the last chapter, choosing between these two patterns largely depends on the scale and depth of your product hierarchies, but also their relationships to one another.
Like the Factory Method, there are a few varieties the Abstract Factory pattern can take depending on what kind of product families you’re creating and what, if any, relationships need to exist between them. Also like the Factory Method pattern in the last chapter, the interfaces or abstract classes give our calling code, or client, the flexibility to not specify the concrete classes of products being created. The added level of abstraction lets the client treat every object factory the same way, regardless of what factory is being used or what family of objects is being instantiated.