Factory 模式提供了在不指定对象确切类的情况下创建对象的方法 — 这是一种方法或类,处理对象创建,决定要实例化哪种类型。它集中并抽象了对象创建,将代码与具体类解耦。
Factory 模式的作用
text
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
示例
js
{
() {
(type) {
: ();
: ();
: ();
}
}
}
shape = .();
