Singleton
| Static class
| |
---|---|---|
Number of access points
| One (and only one) access point - static field Instance
| N (depends on the number of public members of the class and methods)
|
Class inheritance
| Perhaps, but not always (about this - below)
| Impossible - static classes cannot be instantiated because objects of static classes cannot be instantiated
|
Interface Inheritance
| Perhaps without any restrictions
| Impossible for the same reason that class inheritance is impossible
|
Possibility to pass as parameters
| Perhaps because Singleton provides a real object.
| Missing
|
Monitoring the lifetime of the object
| Perhaps - for example, deferred initialization (or creation on demand )
| Impossible for the same reason that class inheritance is impossible
|
Using an abstract factory to create an instance of a class
| maybe
| Impossible due to the absence of the possibility of creating an instance
|
Serialization
| maybe
| Inapplicable due to lack of copy
|
public class Session { private static Session _instance; // ... public static Session Instance { get { // ... return _instance; } } public IUser GetUser() { // ... } public bool IsSessionExpired() { // ... } public Guid SessionID { get { // ... } } }
public static class Session { // 1 public static IUser GetUser() { // ... } // 2 public static bool IsSessionExpired() { // ... } // ... // N public static Guid SessionID { get { // ... } } }
public class Singleton<T> where T : class { private static T _instance; protected Singleton() { } private static T CreateInstance() { ConstructorInfo cInfo = typeof(T).GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[0], new ParameterModifier[0]); return (T)cInfo.Invoke(null); } public static T Instance { get { if (_instance == null) { _instance = CreateInstance(); } return _instance; } } } public class Session : Singleton<Session> { public IUser GetUser() { // ... } public bool IsSessionExpired() { // ... } public Guid SessionID { get { // ... } } }
public class Session : CoreObject { private Session() { } public static Session Instance { get { return Singleton<Session>.Instance; } } }
// ISession public class Session: CoreObject, ISession { private Session() { } public static Session Instance { get { return Singleton<Session>.Instance; } } } // // public class VpnSession : ISession { } public interface ISessionManager { ISession GetSession(Guid sessionID); // ISession, bool IsSessionExpired(ISession session); }
// ... ISessionManager _sessionManager; // ... bool isExpired = _sessionManager.IsSessionExpired(Session.Instance);
public class Singleton<T> where T : class { // ... public static T Instance { get { if (_instance == null) { // " " _instance = CreateInstance(); } return _instance; } } }
public class Singleton<T> where T : class { // ... public static T Instance { // ... } // ! public void RemoveInstance() { _instance = null; } }
public class Singleton<T> where T : class { // ... public static T Instance { get { if (!IsAlive) { // RemoveInstance(); } if (_instance == null) { // " " _instance = CreateInstance(); } return _instance; } } private void RemoveInstance() { _instance = null; } }
public interface IAbstractFactory { T Create<T>(); bool IsSupported<T>(); } public class Singleton<T> where T : class { private static T _instance; private static IAbstractFactory _factory; protected Singleton(IAbstractFactory factory) { _factory = factory; } public static T Instance { get { if (_instance == null) { _instance = _factory.Create<T>(); } return _instance; } } } // public class Session : Singleton<Session> { protected Session() : base(new ConcreteFactory()) { } // ... }
public class Session : CoreObject, ISession { private class SessionSingleton : Singleton<Session> { protected SessionSingleton() : base(new ConcreteFactory2()) { } } private Session() : base(new CoreContext()) { } public static Session Instance { get { return SessionSingleton.Instance; } } // ... }
Source: https://habr.com/ru/post/103681/