Prototype Registry and Object Pools
What you'll learn: How to maintain a registry of pre-configured prototype objects to speed up object creation and reduce initialization costs.
The Core Idea
A Prototype Registry is a centralized collection of pre-made prototype objects that you can clone whenever you need similar instances. Think of it like a cookie cutter drawer in your kitchen—instead of sculpting each cookie by hand, you keep a set of ready-to-use cutters (prototypes) and just press them into the dough.
This pattern extends what you learned about the Prototype Pattern by organizing multiple prototypes in one place where they're easy to find and reuse.
Why Use a Registry?
Without a registry, clients must know how to create or find prototypes themselves. A registry acts as a catalog:
- Named access: Retrieve prototypes by key (e.g., "defaultUser", "adminUser")
- Reduced duplication: Store complex configurations once, clone many times
- Centralized management: One place to update common object templates
Object Pools: Taking It Further
An Object Pool is related but distinct: instead of cloning prototypes, you recycle actual object instances. When you're done with an object, you return it to the pool rather than discarding it. This is especially useful when object creation is expensive (database connections, heavy graphics objects).
The registry tells you what templates exist; the pool tells you which instances are available for reuse.
The Connection
Prototype registries and object pools both solve initialization overhead—one through cloning configured templates, the other through instance reuse. They complement each other: use registries when you need customized copies; use pools when identical, reusable instances suffice.
Key Takeaway: A Prototype Registry organizes reusable object templates by name, reducing the cost of creating commonly-configured objects through cloning rather than expensive initialization.