can we still use this pattern if ConcreteFactory1 needs to createProductC() but ConcreteFactory2 don't?
You have some design options here:
- Create a separate abstract factory for creating
C's. It's often useful break up your abstract factory interfaces by product unlike how the UML diagram has groupedCreateProductAandCreateProductBin the same interface. I think this is the right choice if product C doesn't have much in common with the other products. - Keep
createProductCin the same abstract factory, but change its signature to return eitherProductCor nothing (in Java this would beOption[ProductC]). This will enforce clients to handle the fact that some concrete factories don't produce a product C. Alternatively throw a checked exception. I think this is the right choice if product C is closely related to products A and B and production of C is truly optional, and clients should only call it if it's available on the factory.