简体   繁体   中英

Abstract Factory

I am reading a book named Head First Design Patterns. I am confused about Abstract Factory Pattern. It is said that Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. This is also a point that makes it different with Factory Method.The authors give an example of NYPizzaIngredientFactory, which is a concrete subclass that can create a family of products by calling some methods within the class (such as createDough(), createSauce()). However, I would think that in order to create a specfic Sauce, the class have to rely on concrete classes such as, PlumTomato Sauce or Marirana Sauce. It means that Abstract Factory have to specify concrete classes for these objects. Do I think that right? Please help. Thanks!

Well, the abstract factory itself doesn't have to specify any concrete classes, because it's abstract -- it has no implementation code.

The implementations of the abstract factory do, of course. but:

  • They don't have to tell the user of the factory what they're going to be, nor does the user of the factory have to care what they're going to be. If you ask for a Sauce , then you are guaranteed to receive something which can be used as a Sauce .
  • Because of the former, the actual concrete classes produced by the factory implementation are an implementation detail, and can be changed without having to change the using code.
  • The code that creates the factory isn't the (only) code that uses the factory. So the choice of factory implementation is itself a separated concern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM