Design patterns
Energy.Base.Pattern is a static repository of common design-pattern implementations.
GlobalObject
Provides a process-wide singleton instance. The object is created lazily on first access.
public class MyService
{
public string Name { get; set; }
}
MyService service = Energy.Base.Pattern.GlobalObject<MyService>.Global;
service.Name = "Default";
// Later in the same process
MyService same = Energy.Base.Pattern.GlobalObject<MyService>.Global;
Console.WriteLine(same.Name); // "Default"
GlobalDestroy
Like GlobalObject<T>, but also provides a Destroy() method that disposes the current instance (if it implements IDisposable) so the next access creates a new one.
Energy.Base.Pattern.GlobalDestroy<MyService>.Destroy();
DefaultProperty
Provides a lazy default instance.
Energy.Base.Pattern.DefaultProperty<MyService>.Default.Name = "Default";
Singleton
Provides a singleton instance through an instance wrapper.
Energy.Base.Pattern.Singleton<MyService> singleton = new Energy.Base.Pattern.Singleton<MyService>();
Console.WriteLine(singleton.Instance.Name);
Summary
| Pattern | Description |
|---|---|
GlobalObject<T> |
Lazy global singleton. |
GlobalDestroy<T> |
Global singleton with explicit disposal. |
DefaultProperty<T> |
Lazy default instance. |
Singleton<T> |
Singleton wrapper. |