いつ工場モードを使うべきですか

5102 ワード

あるディスカッションでは、クラスをカプセル化し、ファクトリ・モードを使用してインスタンスを作成する必要があるのはいつなのかについて説明しました.
 
1つの理由は,クラスの構造関数が変化する(名前の変化,パラメータの変化など)場合,ファクトリクラスの1つの関数を変更するだけでよいからである.すべてのコンストラクション関数名をsearchで変更する必要はありません.だから、一般的には、工場モードのパッケージを使用したほうがいいです.
 
やっぱりそうなのかな?
 
ファクトリモードではなく、名前が変更された場合、search&replaceが必要です.しかし、まさか一つのパターンが提案されたのはこの問題を解決するためなのか?!牛刀が鶏を殺したようだね
 
パラメータが変化すると?
関数の転入パラメータが変わったのに、すべてのコードを変更しないと思いますか?!
 
 
定義を見てみましょう
---------------------------
Factory   Methodはオブジェクトを作成するインタフェースを定義する作成モードですが、どのクラスをインスタンス化するかをサブクラスに決定させます.クラスがどの種類のオブジェクトを作成するか、またはサブクラスが作成するオブジェクトを指定する必要があるかを予測できない場合はFactoryを使用する必要があります. 
---------------------------
 
明らかに上の言い方は成立しない.
 
定義で説明したように、作成したインスタンスが予想できない場合にのみ、ファクトリモードが使用されます.すなわち、異なる条件で異なるインスタンスを作成する計画を明確にする場合に使用します.
 
古典的な例は次のとおりです.
 
ログレコーダ:ローカルハードディスク、システムイベント、リモートサーバなどに記録される可能性があります.ユーザーはログをどこに記録するかを選択できます.
 
別の例:
 
≪データベース・アクセス・クラス|Database Access Class|ldap≫:SQLSERVER、ORACLEなどのアクセスが可能で、メモで異なるデータベースへのアクセスを選択できます.
 
次のコードはGEOS 3.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 *newGeoms) const;///Constructs a GeometryCollection with a deep-copy of args GeometryCollection* createGeometryCollection( const std::vector &newGeoms) const;///Construct an EMPTY MultiLineString MultiLineString* createMultiLineString() const;///Construct a MultiLineString taking ownership of given arguments MultiLineString* createMultiLineString( std::vector *newLines) const;///Construct a MultiLineString with a deep-copy of given arguments MultiLineString* createMultiLineString( const std::vector &fromLines) const;///Construct an EMPTY MultiPolygon MultiPolygon* createMultiPolygon() const;///Construct a MultiPolygon taking ownership of given arguments MultiPolygon* createMultiPolygon(std::vector *newPolys) const;///Construct a MultiPolygon with a deep-copy of given arguments MultiPolygon* createMultiPolygon( const std::vector &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 createLinearRing( std::auto_ptr newCoords) const;///Construct a LinearRing with a deep-copy of given arguments LinearRing* createLinearRing( const CoordinateSequence& coordinates) const;///Constructs an EMPTY MultiPoint . MultiPoint* createMultiPoint() const;///Construct a MultiPoint taking ownership of given arguments MultiPoint* createMultiPoint(std::vector *newPoints) const;///Construct a MultiPoint with a deep-copy of given arguments MultiPoint* createMultiPoint( const std::vector &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 *holes) const;///Construct a Polygon with a deep-copy of given arguments Polygon* createPolygon(const LinearRing &shell, const std::vector &holes) const;///Construct an EMPTY LineString LineString* createLineString() const;///Copy a LineString std::auto_ptr createLineString(const LineString& ls) const;///Construct a LineString taking ownership of given argument LineString* createLineString(CoordinateSequence* coordinates) const; std::auto_ptr createLineString( std::auto_ptr coordinates) const;///Construct a LineString with a deep-copy of given argument LineString* createLineString( const CoordinateSequence& coordinates) const;