mardi 28 février 2017

Is Factory method more suitable for frameworks and Abstract facory for Library?

Both Factory method and Abstract factory method are creational pattern. I have studied the Structure and intent as mentioned in GOF.

Factory method pattern uses inheritance, where (an abstract) Factory depends upon its implementing class (to be done later by the client) to create and provide the object of the product.

As per GOF

Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory method lets a class defer instantiation to subclass.

Abstract Factory pattern uses composition, where client depends upon abstract factory to to provide the (related) object(s).

As per GOF

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

my understanding

It seams like for Factory method pattern, the issue it solves is the case when a class does not know the concrete class of the object to use but it wants to proceed with its task. It depends upon the sub classes to provide the object later and with the (super type) Interface of the product it manages to proceed with the work it is supposed to perform.

On the other hand it seams like for Abstract Factory pattern, the issue it solves is when the products are known as their implementations are already there, but need is to prevent the revelation of details of the products to the client. Later on these implementing classes may change. Hence Client s provided with the Abstract factory and its sub classes. As per the concrete factory class object, client gets the product object by the IProduct interface type (Product class implements IProduct). Q1 : is the family of related product necessary , won't this pattern still be relevant if only one kind of product is there with various sub types but not various related types?

Q2. Is my understanding above correct ?

Q3. Above brings another doubt in my mind : Is Factory method more suitable for frameworks and Abstract factory for Library ?

Aucun commentaire:

Enregistrer un commentaire