From what I have read, the abstract factory pattern typically concerns itself with creating several objects which are all associated with the same family, and the factory method pattern concerns itself with generating a single object.
Consider the following example, which flips those concerns:
// Factory Method (base class) allowing for creation of families of objects
public class BasePizzaCreator{
abstract ISauce CreateSauce();
abstract IToppings CreateToppings();
abstract ICrust CreateCrust();
}
// Abstract Factory (interface) defining contract to create a single object
public interface IPizzaFactory{
abstract IPizza CreatePizza();
}
It is obvious that you can use them this way - but is it a violation of the spirit of the patterns? If so, why?
What I really want to understand here is this: Why is Abstract Factory the better approach for creating families of related objects, and Factory method the better approach to creating a single object?
Aucun commentaire:
Enregistrer un commentaire