Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Singleton Pattern</h2> <p><strong>Definition:</strong> If a system only needs one instance of a class, and that instance needs to be accessible in many different parts of a system, you control both instantiation and access by making that class a singleton.</p> <p><strong>Example:</strong> A concrete example would be the <code>ISessionFactory</code> API while using NHibernate. Because of the load in memory the <code>ISessionFactory</code> costs to instantiate, due that it involves all of the object-relation mappings and database connection to be loaded in memory all at once, it is advisable to make only one instance, and create the <code>ISession</code> API instances using this unique instance of the <code>ISessionFactory</code> API, that is, the <em>Singleton Pattern</em>.</p> <pre><code>using NHibernate; internal class NhConnectionFactory { private static readonly ISessionFactory _sessionFactory = config.BuildSessionFactory(); internal NhSystemFactory() { } internal ISession OpenSession(string connectionString) { return _sessionFactory.OpenSession(); } // Other methods here... } public static class NhSystemFacade { private static readonly NhConnectionFactory _nhConnectionFactory = new NhConnectionFactory(); public static string DefaultConnectionString { get { return Properties.Settings.Default.DefaultConnectionString; } } public static ISession OpenSession() { return _nhConnectionFactory.OpenSession(DefaultConnectionString); } } </code></pre> <p><strong>Links:</strong></p> <ul> <li><a href="http://c2.com/cgi/wiki?SingletonPattern" rel="nofollow">Description from the Portland Pattern Repository</a></li> <li><a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">Singleton pattern on Wikipedia</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload