什么时候用工厂模式-----转载

有一次讨论的时候,谈到了这一点:到底什么时候我们需要把类封装,并限制其必须使用工厂模式来创建实例。

一个理由是:当类的构造函数发生变化(名称变化,参数变化等)时,我们只需要更改工厂类中的一个函数就可以了。而不必search所有的构造函数名称并修改之。所以,一般情况下,我们最好都使用工厂模式封装。

果然是这样吗?

不用工厂模式,当名称变化时,确实需要search & replace。但是,难道一个模式被提练出来就是为了解决这个问题?!!似乎牛刀杀鸡了吧

那当 参数变化 时呢?

 函数的转入参数都变了,还想不改动所有的代码吗?!!

先来看看定义

---------------------------

Factory   Method是一种创建性模式,它定义了一个创建对象的接口,但是却让子类来决定具体实例化哪一个类.当一个类无法预料要创建哪种类的对象或是一个类需要由子类来指定创建的对象时我们就需要用到Factory 

---------------------------

显然上面的说法是不成立的。

如定义中说的,只有当无法预料所创建的实例时,才使用工厂模式。即:我们明确的计划不同条件下创建不同实例时,使用它。

经典的一个例子是:

日志记录器:记录可能记录到本地硬盘、系统事件、远程服务器等,用户可以选择记录日志到什么地方。

另一个例子:

数据库访问类:访问可能是SQLSERVER、ORACLE等,用记可以选择创建访问不同的数据库。

下面的代码来自 GEOS3.0.0 (http://geos.refractions.net),非常著名的一个类库,很多软件正在使用它。

看它的实例创建方式正是采用了工厂模式,用户无法直接创建point,line.polygon等实例,而必须通过GeometryFactory类

不过,我个人认为这是对工厂模式的一种滥用:当创建point,line,polygon等实例时,有上面提到的实例不确定性吗?!!

[cpp] view plain copy
 
  1. Point* createPointFromInternalCoord(const Coordinate* coord,  
  2.         const Geometry *exemplar) const;  
  3. /// Converts an Envelope to a Geometry.  
  4. //  
  5. /// Returned Geometry can be a Point, a Polygon or an EMPTY geom.  
  6. ///  
  7. Geometry* toGeometry(const Envelope* envelope) const;  
  8. /// /brief  
  9. /// Returns the PrecisionModel that Geometries created by this  
  10. /// factory will be associated with.  
  11. const PrecisionModel* getPrecisionModel() const;  
  12. /// Creates an EMPTY Point  
  13. Point* createPoint() const;  
  14. /// Creates a Point using the given Coordinate  
  15. Point* createPoint(const Coordinate& coordinate) const;  
  16. /// Creates a Point taking ownership of the given CoordinateSequence  
  17. Point* createPoint(CoordinateSequence *coordinates) const;  
  18. /// Creates a Point with a deep-copy of the given CoordinateSequence.  
  19. Point* createPoint(const CoordinateSequence &coordinates) const;  
  20. /// Construct an EMPTY GeometryCollection  
  21. GeometryCollection* createGeometryCollection() const;  
  22. /// Construct the EMPTY Geometry  
  23. Geometry* createEmptyGeometry() const;  
  24. /// Construct a GeometryCollection taking ownership of given arguments  
  25. GeometryCollection* createGeometryCollection(  
  26.         std::vector<Geometry *> *newGeoms) const;  
  27. /// Constructs a GeometryCollection with a deep-copy of args  
  28. GeometryCollection* createGeometryCollection(  
  29.         const std::vector<Geometry *> &newGeoms) const;  
  30. /// Construct an EMPTY MultiLineString   
  31. MultiLineString* createMultiLineString() const;  
  32. /// Construct a MultiLineString taking ownership of given arguments  
  33. MultiLineString* createMultiLineString(  
  34.         std::vector<Geometry *> *newLines) const;  
  35. /// Construct a MultiLineString with a deep-copy of given arguments  
  36. MultiLineString* createMultiLineString(  
  37.         const std::vector<Geometry *> &fromLines) const;  
  38. /// Construct an EMPTY MultiPolygon   
  39. MultiPolygon* createMultiPolygon() const;  
  40. /// Construct a MultiPolygon taking ownership of given arguments  
  41. MultiPolygon* createMultiPolygon(std::vector<Geometry *> *newPolys) const;  
  42. /// Construct a MultiPolygon with a deep-copy of given arguments  
  43. MultiPolygon* createMultiPolygon(  
  44.         const std::vector<Geometry *> &fromPolys) const;  
  45. /// Construct an EMPTY LinearRing   
  46. LinearRing* createLinearRing() const;  
  47. /// Construct a LinearRing taking ownership of given arguments  
  48. LinearRing* createLinearRing(CoordinateSequence* newCoords) const;  
  49. std::auto_ptr<Geometry> createLinearRing(  
  50.         std::auto_ptr<CoordinateSequence> newCoords) const;  
  51. /// Construct a LinearRing with a deep-copy of given arguments  
  52. LinearRing* createLinearRing(  
  53.         const CoordinateSequence& coordinates) const;  
  54. /// Constructs an EMPTY <code>MultiPoint</code>.  
  55. MultiPoint* createMultiPoint() const;  
  56. /// Construct a MultiPoint taking ownership of given arguments  
  57. MultiPoint* createMultiPoint(std::vector<Geometry *> *newPoints) const;  
  58. /// Construct a MultiPoint with a deep-copy of given arguments  
  59. MultiPoint* createMultiPoint(  
  60.         const std::vector<Geometry *> &fromPoints) const;  
  61. /// /brief  
  62. /// Construct a MultiPoint containing a Point geometry  
  63. /// for each Coordinate in the given list.  
  64. MultiPoint* createMultiPoint(  
  65.         const CoordinateSequence &fromCoords) const;  
  66. /// Construct an EMPTY Polygon   
  67. Polygon* createPolygon() const;  
  68. /// Construct a Polygon taking ownership of given arguments  
  69. Polygon* createPolygon(LinearRing *shell,  
  70.         std::vector<Geometry *> *holes) const;  
  71. /// Construct a Polygon with a deep-copy of given arguments  
  72. Polygon* createPolygon(const LinearRing &shell,  
  73.         const std::vector<Geometry *> &holes) const;  
  74. /// Construct an EMPTY LineString   
  75. LineString* createLineString() const;  
  76. /// Copy a LineString  
  77. std::auto_ptr<LineString> createLineString(const LineString& ls) const;  
  78. /// Construct a LineString taking ownership of given argument  
  79. LineString* createLineString(CoordinateSequence* coordinates) const;  
  80. std::auto_ptr<Geometry> createLineString(  
  81.         std::auto_ptr<CoordinateSequence> coordinates) const;  
  82. /// Construct a LineString with a deep-copy of given argument  
  83. LineString* createLineString(  
  84.         const CoordinateSequence& coordinates) const;  
原文地址:https://www.cnblogs.com/kaililikai/p/5859183.html