Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider usage of Creational patterns, described in GOF (<a href="http://en.wikipedia.org/wiki/Design_Patterns#Creational_patterns" rel="nofollow">"Gang Of Four"</a>)</p> <p>There are the following ways:</p> <p>1) If you want to have only one instance of object to be created, use <a href="http://en.wikipedia.org/wiki/Singleton_pattern" rel="nofollow">Singleton</a> </p> <p>There is a good example of thread-safe singleton on <a href="http://msdn.microsoft.com/en-us/library/ff650316.aspx" rel="nofollow">MSDN</a></p> <blockquote> <p>In this strategy, the instance is created the first time any member of the class is referenced</p> </blockquote> <pre><code>public sealed class Singleton { private static readonly Singleton instance = new Singleton(); private Singleton(){} public static Singleton Instance { get { return instance; } } } </code></pre> <p>2) If you don't want to specify the exact class to create, use <a href="http://en.wikipedia.org/wiki/Factory_method_pattern" rel="nofollow">Factory method</a></p> <p>Here is an extract from an article on C#-Corner <a href="http://www.c-sharpcorner.com/uploadfile/kalisk/factory-method-design-pattern-using-C-Sharp/" rel="nofollow">Factory method Design pattern using C#</a></p> <pre><code>abstract class Factory { public abstract Product GetProduct(); //Factory Method Declaration } class concreteFactoryforProcuct1 : Factory { public override Product GetProduct() //Factory Method Implementation { return new Product1(); } } </code></pre> <p>3) If there is a group of objects to be created this way, use <a href="http://en.wikipedia.org/wiki/Abstract_factory_pattern" rel="nofollow">Abstract factory</a></p> <p>Here are extracts from an article on codeproject: <a href="http://www.codeproject.com/Articles/328373/Understanding-and-Implementing-Abstract-Factory-Pa" rel="nofollow">Understanding and implementing abstract factory pattern in C#</a> </p> <p>Creating the Abstract Factory</p> <pre><code>interface IPhoneFactory //'I' stands for interface no relation with Iphone { ISmart GetSmart(); IDumb GetDumb(); } </code></pre> <p>Creating the Concrete Factories</p> <pre><code>class SamsungFactory : IPhoneFactory { public ISmart GetSmart() { return new GalaxyS2(); } public IDumb GetDumb() { return new Primo(); } } ... </code></pre> <p>Creating the Client</p> <pre><code>enum MANUFACTURERS { SAMSUNG, HTC, NOKIA } class PhoneTypeChecker { IPhoneFactory factory; ... public PhoneTypeChecker(MANUFACTURERS m) { m_manufacturer= m; } public void CheckProducts() { switch (m_manufacturer) { case MANUFACTURERS.SAMSUNG: factory = new SamsungFactory(); break; case MANUFACTURERS.HTC: factory = new HTCFactory(); break; case MANUFACTURERS.NOKIA: factory = new NokiaFactory(); break; } ... factory.GetSmart(); factory.GetDumb(); ... } } static void Main(string[] args) { PhoneTypeChecker checker = new PhoneTypeChecker(MANUFACTURERS.SAMSUNG); checker.CheckProducts(); ... } </code></pre> <p>4) Use you common sense to develop your own design that would satisfy your needs.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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