Head First Design Patterns - Factory Method Pattern

The Factory Method Pattern defines an interface for creating an object, but lets subclassed decide which class

to instantiate. Factory Method lets a class defer instantiation to subclasses.

Dependency Inversion Principle

  • Depend upon abstractions. Do not depenfd upon concrete classes

Guidelies to follow the principle

  • No variable should hold a reference to a concrete class
  • No class should derive from a concrete class
  • No method should override an implemented method of any of its base classes

The Abstract Factory Pattern provides an interface for creating families of related or dependent

objects without specifying their concrete classed.

Factory Method VS. Abstract Method

工厂方法模式

  • 一个抽象产品类,可以派生出多个具体产品类
  •  一个抽象工厂类,可以派生出多个具体工厂类
  • 每个具体工厂类只能创建一个具体产品类的实例

抽象工厂模式

  • 多个抽象产品类,每个抽象产品类可以派生出多个具体产品类
  • 一个抽象工厂类,可以派生出多个具体工厂类
  • 每个具体工厂类可以创建多个具体产品类的实例。

下面是两篇帮助理解的工厂方法和抽象工厂模式的文章:

http://laughingchs.iteye.com/blog/1169986

http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html

posted @ 2012-05-05 17:47  qiangzhu  阅读(510)  评论(0编辑  收藏  举报