1

I have read Factory Method pattern. I have a problem when design the latter example.

Best design for the pattern? Anybody can give me any suggestions?

     AbstractProduct                   Creator
         /     \                        /   \
    Product1 Product2             Creator1 Creator2



        AbstractProduct                         ? How to design Creator ?
       /     |         \
Product1 Product2 SubAbstractProduct
                       /     \
                  Product3 Product 4
4

1 回答 1

1

Creator can be designed the same way as in the previous example because Concrete Creators that create Product3 and Product4 still are AbstractProduct.

Answering the comment:

Not really. When this AbstractProduct createProduct() is overridden in child Concrete Creator classes (which is the factory pattern), it can return SubAbstractProduct.

Just wanted to add that you can do the above (i.e. overridden method returning a subtype of what is returned in the parent class method)in java since Java 1.5.

于 2012-07-02T05:49:53.960 回答