创建型模式 (Creational patterns) 处理对象的创建 — 对象如何被实例化、如何变得灵活,以及如何与使用它们的代码解耦。它们包括 Singleton、Factory、Builder、Prototype 和 Abstract Factory。
创建型模式解决的问题
CREATIONAL patterns abstract and control HOW objects are CREATED:
→ instead of directly calling constructors everywhere (rigid, coupled), creational
patterns make object creation flexible, decoupled, and manageable
→ they help create objects in a way suited to the situation, decoupled from concrete classes
主要的创建型模式
SINGLETON → ensure only ONE instance of a class (single shared object)
FACTORY METHOD → create objects without specifying the exact class (defer to a method/subclass)
ABSTRACT FACTORY → create FAMILIES of related objects (without specifying concrete classes)
BUILDER → construct complex objects step-by-step (separate construction from representation);
good for objects with many optional parts/parameters
PROTOTYPE → create new objects by CLONING an existing one (copy a prototype)
