Design patterns are reusable solutions to problems that recur again and again in software design. Introduced formally by the Gang of Four (GoF) in their landmark 1994 book, each pattern has four essential elements: a name, a description of the problem it solves, the solution structure, and the consequences of applying it. Kimera Core draws on four patterns — Singleton, Strategy, Template Method, and Decorator — to keep its cache and serialization subsystems modular, testable, and easy to extend. This page explains what each pattern is, where it appears in the library, and how to apply it in your own code.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/ismael-sarmiento/kimera_python/llms.txt
Use this file to discover all available pages before exploring further.
Creational Patterns
Creational patterns deal with how objects are instantiated. They isolate client code from the details of construction so that the same interface works regardless of which concrete class is created.Singleton
Guarantee that a class has only one instance and provide a global access point to it.The Singleton pattern is particularly important for
InMemoryCacheEngine. Because in-memory caches are backed by a plain Python dict attached to the instance, every call to InMemoryCacheEngine() that creates a new object also creates a new, empty cache — defeating the purpose of caching entirely. Wrapping the class with a Singleton implementation ensures that all callers share the same dict.
The
engines.py source file includes a comment: # TODO: para que funcione la clase InMemoryCacheEngine debe ser Singleton. The four implementations below are drop-in solutions for that requirement.1. Decorator by Function
1. Decorator by Function
A plain function wraps the class and maintains a private Pros
instances dict. When the decorated class is “instantiated”, the wrapper checks the dict first.- Decorators compose naturally; easy to add or remove without touching the class body.
- Minimal boilerplate.
InMemoryCacheEngineis now a function, not a class —isinstance()checks and class-method calls on the name itself will not work.type(n)()bypasses the singleton guard and creates a new instance.
2. Base Class
2. Base Class
A Pros
Singleton metaclass overrides __call__ so that any class that uses it as its metaclass automatically enforces single-instance semantics.MyClassremains a true class;isinstance()andissubclass()work correctly.
- Multiple inheritance is tricky — if a second base class also defines
__new__or uses its own metaclass, conflicts must be resolved explicitly.
3. Metaclass
3. Metaclass
The metaclass approach uses the metaclass hook for controlling class instantiation, and it automatically handles inheritance for all subclasses.Pros
- True class; all reflection and introspection work normally.
- Inheritance is handled automatically — subclasses of
MyClassalso become singletons. - Uses the metaclass hook for its intended purpose.
- No known disadvantages.
4. Decorator Returning a Class
4. Decorator Returning a Class
A decorator wraps the target class in a new class that carries the singleton logic, then renames the wrapper to match the original.No specific pros or cons have been identified for this approach beyond those of the function-decorator variant above.
Structural and Behavioral Patterns in Kimera Core
Strategy — Swappable Cache Engines
The Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Client code programs against an abstract interface and selects a concrete strategy at runtime. In Kimera Core,BaseCacheEngine is the strategy interface. InMemoryCacheEngine and ThreadCacheEngine are concrete strategies. Swapping the engine requires changing only one constructor call — no other code changes.
Template Method — LocalCacheEngine Subclassing
The Template Method pattern defines the skeleton of an algorithm in a base class and defers the steps that vary to subclasses. The base class calls abstract methods that each subclass fills in.LocalCacheEngine plays this role in Kimera Core. It provides the concrete helper methods normalize_timeout and normalize_size that all engines rely on, while declaring size_regulator as abstract. Concrete engines (InMemoryCacheEngine, ThreadCacheEngine) implement size_regulator and all five CRUD methods to complete the algorithm.
Decorator — Transparent Caching for Any Function
The Decorator pattern attaches additional responsibilities to an object dynamically, without modifying the object’s interface. In Python this maps directly onto function decorators.@thread_local_cache and @in_memory_local_cache wrap any function so that its return value is automatically stored in the chosen cache engine after each call. The wrapped function’s signature and return value are completely unchanged from the caller’s perspective.
Pattern Classification Table
| Pattern | Category | Where Used in Kimera Core |
|---|---|---|
| Singleton | Creational | Recommended for InMemoryCacheEngine shared instances |
| Strategy | Behavioral | Cache engine selection (BaseCacheEngine implementations) |
| Template Method | Behavioral | LocalCacheEngine subclassing (size_regulator, CRUD methods) |
| Decorator | Structural | @thread_local_cache, @in_memory_local_cache |