function get_manual_relationship_manager
Returns a singleton instance of the ManualRelationshipManager class, creating it on first access using lazy initialization.
/tf/active/vicechatdev/full_smartstat/manual_relationships.py
158 - 163
simple
Purpose
This function implements the singleton pattern to provide global access to a single ManualRelationshipManager instance throughout the application. It ensures that only one manager exists, which is useful for maintaining consistent state when managing manual relationships between entities. The lazy initialization approach defers object creation until first use, optimizing resource usage.
Source Code
def get_manual_relationship_manager() -> ManualRelationshipManager:
"""Get the global manual relationship manager instance."""
global _manager
if _manager is None:
_manager = ManualRelationshipManager()
return _manager
Return Value
Type: ManualRelationshipManager
Returns a ManualRelationshipManager instance. This is always the same singleton instance across all calls to this function. The ManualRelationshipManager is responsible for managing manual relationships, likely between data entities or objects in the system.
Required Imports
from typing import Optional
Usage Example
# Assuming ManualRelationshipManager class is defined
# and global _manager variable exists
# Get the singleton instance
manager = get_manual_relationship_manager()
# Use the manager (example methods depend on ManualRelationshipManager implementation)
# manager.add_relationship(entity1, entity2)
# manager.get_relationships(entity1)
# Subsequent calls return the same instance
manager2 = get_manual_relationship_manager()
assert manager is manager2 # True - same instance
Best Practices
- This function relies on a module-level global variable '_manager' that must be initialized (typically as None) before this function is called
- The singleton pattern can make testing difficult; consider dependency injection for better testability
- Thread safety is not guaranteed in this implementation; if used in multi-threaded contexts, consider adding locks
- The global state makes the code harder to reason about; use sparingly and document where the manager is accessed
- Ensure ManualRelationshipManager class is properly defined before calling this function
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class ManualRelationshipManager 63.9% similar
-
function get_driver 51.9% similar
-
function add_manual_relationship 51.3% similar
-
function create_training_management 49.5% similar
-
class Neo4jManager 45.9% similar