什么时候应该使用 工厂模式
2009-08-25 15:20
525 查看
有一次讨论的时候,谈到了这一点:到底什么时候我们需要把类封装,并限制其必须使用工厂模式来创建实例。
一个理由是:当类的构造函数发生变化(名称变化,参数变化等)时,我们只需要更改工厂类中的一个函数就可以了。而不必search所有的构造函数名称并修改之。所以,一般情况下,我们最好都使用工厂模式封装。
果然是这样吗?
不用工厂模式,当名称变化时,确实需要search & replace。但是,难道一个模式被提练出来就是为了解决这个问题?!!似乎牛刀杀鸡了吧
那当 参数变化 时呢?
函数的转入参数都变了,还想不改动所有的代码吗?!!
先来看看定义
---------------------------
Factory Method是一种创建性模式,它定义了一个创建对象的接口,但是却让子类来决定具体实例化哪一个类.当一个类无法预料要创建哪种类的对象或是一个类需要由子类来指定创建的对象时我们就需要用到Factory
---------------------------
显然上面的说法是不成立的。
如定义中说的,只有当无法预料所创建的实例时,才使用工厂模式。即:我们明确的计划不同条件下创建不同实例时,使用它。
经典的一个例子是:
日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。
另一个例子:
数据库访问类:访问可能是SQLSERVER、ORACLE等,用记可以选择创建访问不同的数据库。
下面的代码来自 GEOS3.0.0 (http://geos.refractions.net),非常著名的一个类库,很多软件正在使用它。
看它的实例创建方式正是采用了工厂模式,用户无法直接创建point,line.polygon等实例,而必须通过GeometryFactory类
不过,我个人认为这是对工厂模式的一种滥用:当创建point,line,polygon等实例时,有上面提到的实例不确定性吗?!!
一个理由是:当类的构造函数发生变化(名称变化,参数变化等)时,我们只需要更改工厂类中的一个函数就可以了。而不必search所有的构造函数名称并修改之。所以,一般情况下,我们最好都使用工厂模式封装。
果然是这样吗?
不用工厂模式,当名称变化时,确实需要search & replace。但是,难道一个模式被提练出来就是为了解决这个问题?!!似乎牛刀杀鸡了吧
那当 参数变化 时呢?
函数的转入参数都变了,还想不改动所有的代码吗?!!
先来看看定义
---------------------------
Factory Method是一种创建性模式,它定义了一个创建对象的接口,但是却让子类来决定具体实例化哪一个类.当一个类无法预料要创建哪种类的对象或是一个类需要由子类来指定创建的对象时我们就需要用到Factory
---------------------------
显然上面的说法是不成立的。
如定义中说的,只有当无法预料所创建的实例时,才使用工厂模式。即:我们明确的计划不同条件下创建不同实例时,使用它。
经典的一个例子是:
日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。
另一个例子:
数据库访问类:访问可能是SQLSERVER、ORACLE等,用记可以选择创建访问不同的数据库。
下面的代码来自 GEOS3.0.0 (http://geos.refractions.net),非常著名的一个类库,很多软件正在使用它。
看它的实例创建方式正是采用了工厂模式,用户无法直接创建point,line.polygon等实例,而必须通过GeometryFactory类
不过,我个人认为这是对工厂模式的一种滥用:当创建point,line,polygon等实例时,有上面提到的实例不确定性吗?!!
Point* createPointFromInternalCoord(const Coordinate* coord, const Geometry *exemplar) const; /// Converts an Envelope to a Geometry. // /// Returned Geometry can be a Point, a Polygon or an EMPTY geom. /// Geometry* toGeometry(const Envelope* envelope) const; /// /brief /// Returns the PrecisionModel that Geometries created by this /// factory will be associated with. const PrecisionModel* getPrecisionModel() const; /// Creates an EMPTY Point Point* createPoint() const; /// Creates a Point using the given Coordinate Point* createPoint(const Coordinate& coordinate) const; /// Creates a Point taking ownership of the given CoordinateSequence Point* createPoint(CoordinateSequence *coordinates) const; /// Creates a Point with a deep-copy of the given CoordinateSequence. Point* createPoint(const CoordinateSequence &coordinates) const; /// Construct an EMPTY GeometryCollection GeometryCollection* createGeometryCollection() const; /// Construct the EMPTY Geometry Geometry* createEmptyGeometry() const; /// Construct a GeometryCollection taking ownership of given arguments GeometryCollection* createGeometryCollection( std::vector<Geometry *> *newGeoms) const; /// Constructs a GeometryCollection with a deep-copy of args GeometryCollection* createGeometryCollection( const std::vector<Geometry *> &newGeoms) const; /// Construct an EMPTY MultiLineString MultiLineString* createMultiLineString() const; /// Construct a MultiLineString taking ownership of given arguments MultiLineString* createMultiLineString( std::vector<Geometry *> *newLines) const; /// Construct a MultiLineString with a deep-copy of given arguments MultiLineString* createMultiLineString( const std::vector<Geometry *> &fromLines) const; /// Construct an EMPTY MultiPolygon MultiPolygon* createMultiPolygon() const; /// Construct a MultiPolygon taking ownership of given arguments MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const; /// Construct a MultiPolygon with a deep-copy of given arguments MultiPolygon* createMultiPolygon( const std::vector<Geometry *> &fromPolys) const; /// Construct an EMPTY LinearRing LinearRing* createLinearRing() const; /// Construct a LinearRing taking ownership of given arguments LinearRing* createLinearRing(CoordinateSequence* newCoords) const; std::auto_ptr<Geometry> createLinearRing( std::auto_ptr<CoordinateSequence> newCoords) const; /// Construct a LinearRing with a deep-copy of given arguments LinearRing* createLinearRing( const CoordinateSequence& coordinates) const; /// Constructs an EMPTY <code>MultiPoint</code>. MultiPoint* createMultiPoint() const; /// Construct a MultiPoint taking ownership of given arguments MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const; /// Construct a MultiPoint with a deep-copy of given arguments MultiPoint* createMultiPoint( const std::vector<Geometry *> &fromPoints) const; /// /brief /// Construct a MultiPoint containing a Point geometry /// for each Coordinate in the given list. MultiPoint* createMultiPoint( const CoordinateSequence &fromCoords) const; /// Construct an EMPTY Polygon Polygon* createPolygon() const; /// Construct a Polygon taking ownership of given arguments Polygon* createPolygon(LinearRing *shell, std::vector<Geometry *> *holes) const; /// Construct a Polygon with a deep-copy of given arguments Polygon* createPolygon(const LinearRing &shell, const std::vector<Geometry *> &holes) const; /// Construct an EMPTY LineString LineString* createLineString() const; /// Copy a LineString std::auto_ptr<LineString> createLineString(const LineString& ls) const; /// Construct a LineString taking ownership of given argument LineString* createLineString(CoordinateSequence* coordinates) const; std::auto_ptr<Geometry> createLineString( std::auto_ptr<CoordinateSequence> coordinates) const; /// Construct a LineString with a deep-copy of given argument LineString* createLineString( const CoordinateSequence& coordinates) const;
相关文章推荐
- 什么时候应该使用SQL Server锁模式
- 使用(function() {}).call(this);包裹代码有什么好处,什么时候应该这样做?
- 什么时候应该使用 ==?什么时候应该使用 Equals?
- 什么时候应该使用Autorelease Pool
- 什么时候应该使用反射?
- 什么时候应该使用 ==?什么时候应该使用 Equals?
- 什么时候应该使用OLE-DB,ADO 或ODBC
- 你知道什么时候应该使用Web Service吗?
- hibernate之什么时候应该使用悲观锁?
- 什么时候应该使用Web Service
- 在重构的过程中,什么情况下应该使用设计模式?应该用哪种模式?
- 什么时候应该使用$scope.$apply()
- 数组(Array)和列表(ArrayList)的区别?什么时候应该使用Array?
- 什么时候应该使用 ==?什么时候应该使用 Equals?
- 什么时候应该使用Web Service
- 什么时候应该使用索引,应该使用什么类型的索引
- 什么时候应该使用Web Service
- 什么时候应该使用OLE-DB,ADO或ODBC
- 有哪些类加载器?解释什么是双亲委派模型,为什么使用双亲委派模型,如何自定义类加载器?什么时候应该自定义类加载器?
- 什么时候应该使用Web Service? (我想问,如果在局域网里使用web service+windows程序来开发原来的C/S结构的程序,性能上差多少?)