PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 9 ■ GENERATING OBJECTS

Figure 9-5. Extending the design to support a new protocol


Consequences


Notice that the creator classes mirror the product hierarchy. This is a common consequence of the
Factory Method pattern and disliked by some as a special kind of code duplication. Another issue is the
possibility that the pattern could encourage unnecessary subclassing. If your only reason for subclassing
a creator is to deploy the Factory Method pattern, you may need to think again (that’s why I introduced
the header and footer constraints to our example here).
I have focused only on appointments in my example. If I extend it somewhat to include to-do items
and contacts, I face a new problem. I need a structure that will handle sets of related implementations at
one time. The Factory Method pattern is often used with the Abstract Factory pattern, as you will see in
the next section.


Abstract Factory Pattern


In large applications, you may need factories that produce related sets of classes. The Abstract Factory
pattern addresses this problem.

Free download pdf