CenterBase
Coding Style wiki
using OxGFrame.CenterFrame;
Core Design Concept
CenterBase<TCenter, TClass> adopts the CRTP (Curiously Recurring Template Pattern) and Thread-Safe Singleton design. It establishes a type-safe cache pool, providing unified object management logic for all center points inheriting from this class.
Default APIs (Static Interface)
The following methods are static interfaces and can be called directly via the subclass name (e.g., YourCenter.Add<YourClass>()).
Register (Add)
Instantiates objects and adds them to cache management.
- Add<UClass>()
- Registers using the Type Hash of
UClassas the ID.
- Registers using the Type Hash of
- Add<UClass>(int id)
- Registers a
UClassinstance with a specificid.
- Registers a
- Add(int id, TClass @class)
- Directly adds an existing instance to the cache with a specified
id.
- Directly adds an existing instance to the cache with a specified
Reminder When using generic registration, the class must have a parameterless constructor (new()).
Remove (Unload)
Removes objects from the cache pool.
- Delete<UClass>()
- Removes the corresponding instance based on type.
- Delete(int id)
- Removes the instance based on the specified
id.
- Removes the instance based on the specified
- DeleteAll()
- Clears all objects in the cache pool.
Get (Find/Retrieve)
Retrieves objects from the cache pool and automatically casts them.
- Find<UClass>()
- Finds an object based on type.
- Find<UClass>(int id)
- Finds an object based on the specified
idand casts it to theUClasstype.
- Finds an object based on the specified
Internal Implementation Details
Thread-Safe Singleton
Built-in Double-Checked Locking ensures the uniqueness and safety of GetInstance() in multi-threaded environments.
Cache System
The underlying storage uses Dictionary<int, TClass>:
- Key: Unique identifier of the object (ID or Type Hash).
- Value: Instantiated object.
Important CenterBase is only responsible for the storage lifecycle of objects. If the managed objects involve Unity resources (such as Prefabs), please ensure proper destruction in conjunction with AssetLoaders.