PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1
CHAPTER 9 ■ GENERATING OBJECTS

Figure 9-9. Adding concrete creators and some concrete products


Consequences


So what does this pattern buy you?



  • First, you decouple your system from the details of implementation. I can add or
    remove any number of encoding formats in my example without causing a knock
    on effect.

  • You enforce the grouping of functionally related elements of your system. So by
    using BloggsCommsManager, I am guaranteed that I will work only with BloggsCal-
    related classes.

  • Adding new products can be a pain. Not only do I have to create concrete
    implementations of the new product but also we have to amend the abstract
    creator and every one of its concrete implementers in order to support it.
    Many implementations of the Abstract Factory pattern use the Factory Method pattern. This may be
    because most examples are written in Java or C++. PHP, however, does not enforce a return type for a
    method, which affords us some flexibility that we might leverage.
    Rather than create separate methods for each Factory Method, you can create a single make()
    method that uses a flag argument to determine which object to return:

Free download pdf