Principle:Tensorflow Serving Class Registration Pattern
| Knowledge Sources | |
|---|---|
| Domains | Design Pattern |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
A macro-driven factory registration pattern that maps protobuf configuration message types to subclass factory methods, enabling extensible, config-driven object instantiation without modifying existing code.
Description
The Class Registration Pattern provides a decoupled way to register and instantiate subclasses of an abstract base class using protobuf configuration messages as the dispatch key. A registry is defined once using DEFINE_CLASS_REGISTRY, establishing a mapping from config proto message type names to factory objects. Subclass implementations register themselves via REGISTER_CLASS macros, which create static initializers that populate the global factory map at program startup. The registry can then create instances given a config proto (by looking up its message type name) or an Any proto (by unwrapping and dispatching). The pattern supports additional factory parameters beyond the config proto, enabling parameterized construction. Thread safety is ensured via mutex-protected map access. This approach follows the Service Locator and Factory Method design patterns, adapted for protobuf-based configuration.
Usage
Use this pattern when building a plugin-like architecture where new implementations should be registrable without modifying existing code, and where the choice of implementation is determined by a protobuf configuration at runtime. It is the foundation for extensibility in TensorFlow Serving's source adapters, storage path sources, and platform implementations.
Theoretical Basis
This pattern combines several well-known design patterns: the Factory Method pattern (deferred construction to subclasses), the Registry pattern (centralized lookup of implementations), and the Service Locator pattern (runtime resolution of dependencies). The use of C++ static initialization for registration is a common technique in plugin architectures (similar to the "self-registering factory" idiom). The protobuf Any type serves as a universal envelope for type-discriminated configuration, similar to the Envelope/Letter pattern.