Change log

Home

Singleton pattern

Links

Een thread-safe singleton baseclass in C# met SingletonFactory. (versie 2)
namespace DesignPatterns
{
    
public interface ISingleton
{
// not much
}
    
internal class SingletonException : Exception 
{
public SingletonException(string message): base(message){}
}
  
public class SingletonFactory
{
static private Hashtable singletonTable = new Hashtable();
static private TypeFilter singletonFilter = new TypeFilter(SingletonInterfaceFilter);
    
    
private static bool SingletonInterfaceFilter(Type typeObject, Object criteriaObject)
{
    if(typeObject.ToString() == criteriaObject.ToString())
    return true;
    else
    return false;
}
    
static protected ISingleton Instance(Type singletonType)
{
    Object _singleton = null;
    string WarningsMessage = ": class must implement ISingleton interface";
       
    Type[] singletonInterfaces = singletonType.FindInterfaces(singletonFilter
                                                            , "DesignPatterns.ISingleton");
                                                                
    if (singletonInterfaces.Length == 0)
    throw new SingletonException(singletonType.ToString() + WarningsMessage);
        
    Mutex mutex = new Mutex();
    mutex.WaitOne();

    _singleton = singletonTable[singletonType];
    if (_singleton == null)
    {
    _singleton = Activator.CreateInstance(singletonType);
    singletonTable.Add(singletonType, _singleton);
    }
        
    mutex.Close();
      
    return _singleton as ISingleton;
}
}
}

Een SingletonFactory implementatie.
using DesignPatterns;

namespace MyDesignPatterns
{
public interface IMyBaseSingleton : ISingleton
{
string name { get; set; }
}
  
public interface ISingletonA : IMyBaseSingleton
{ 
// ISingletonA interface definitions
}

public interface ISingletonB : IMyBaseSingleton
{
// ISingletonB interface definitions
}
  
internal class MyBaseSingleton : IMyBaseSingleton
{
private string _name;
public  string  name { get {return _name;} set {_name = value;}}
}
    
internal class SingletonA : MyBaseSingleton, ISingletonA
{
// SingletonA implementation
}
    
internal class SingletonB : MyBaseSingleton, ISingletonB
{
// SingletonB implementation
}
  
public class MySingletonFactory : SingletonFactory
{
public MySingletonFactory()
{
}
    
static public ISingletonA singletonA()
{
    return Instance(typeof(SingletonA)) as ISingletonA;
}
      
static public ISingletonB singletonB()
{
    return Instance(typeof(SingletonB)) as ISingletonB;
}
}
}

En een stukje code om te zien wat het doet.
    private string PerformTest()
{
    ISingletonA TestSingletonA  = MySingletonFactory.singletonA();
    ISingletonB TestSingletonB1 = MySingletonFactory.singletonB();
    ISingletonB TestSingletonB2 = MySingletonFactory.singletonB();
      
    TestSingletonB2.name = "B2";
    TestSingletonB1.name = "B1";
    TestSingletonA.name  = "A";
      
    return "value of name in A/B1/B2 : " 
            + TestSingletonA.name + "/" 
            + TestSingletonB1.name + "/" 
            + TestSingletonB2.name;
}
Top
17 oktober 2004 Download gerepareerd.
9 oktober 2004 Sample applicatie om te dowloaden toegevoegd.
6 oktober 2004 Versie met 1 verhoogd. Singleton code versie 1
Singleton via interface teruggeven vanuit de factory.
Alleen voorbeeld code die van toepassing is tonen.
2 oktober 2004 CSharp Singleton code page naar nieuwe url overgezet, design aangepast.

21-04-2018/18-05-2024

konfidence in it (c) 2004