Factory パターンは、正確なクラスを指定せずにオブジェクトを作成する方法を提供します。オブジェクト作成を処理し、どの型をインスタンス化するかを決定するメソッドまたはクラスです。オブジェクト作成を一元化および抽象化し、具体的なクラスからコードを疎結合にします。
Factory パターンが行うこと
FACTORY → encapsulate object CREATION (instead of calling constructors directly):
→ a factory method/class decides WHICH class to instantiate and creates it
→ the caller asks the factory for an object (by type/parameter) → gets the right object
WITHOUT knowing/specifying the concrete class
→ decouples code from CONCRETE classes; centralizes creation logic
例
{
() {
(type) {
: ();
: ();
: ();
}
}
}
shape = .();
