#region Copyright
///Copyright 2008 Codesagar.
///DISCLAIMER: Author claims no responsibility for any damages whatsoever
///stemming out either directly or indirectly from the usage of any of the
///source code made available through Codesagar.
#endregion
using System;
namespace Factories
{
/// <summary>
/// Usage: TConcreteFactory is the concrete factory that implements this base
/// class and creates instances of type TCreatedType (and its derived classes)
/// </summary>
/// <typeparam name="TConcreteFactory">Concrete factory type</typeparam>
/// <typeparam name="TProduct">Product type to be created by the
/// concrete factory
/// </typeparam>
public class AbstractFactory<TConcreteFactory, TProduct> :
SingletonBase<TConcreteFactory>
where TConcreteFactory : class, new()
where TProduct : class
{
/// <summary>
/// This is the factory class that must be implemented by your concrete
/// factory instance.
/// </summary>
/// <param name="args">Arguments that your implementation of Create
/// can accept.
/// </param>
/// <returns>Return the instance of your desired type TProduct or its
/// subtype
/// </returns>
public virtual TProduct Create(params object[] args)
{
throw new NotImplementedException();
}
}
class ExampleProduct { }
class ExampleFactory : AbstractFactory<ExampleFactory, ExampleFactory>
{
public override ExampleFactory Create(params object[] args)
{
return new ExampleFactory();
}
}
class ExampleClient
{
public void Demonstrate()
{
ExampleFactory.Instance.Create();
}
}
}
Showing posts with label Singleton. Show all posts
Showing posts with label Singleton. Show all posts
Tuesday, December 9, 2008
Monday, December 8, 2008
Dish #1: Generic Singleton base class
#region Copyright
///Copyright 2008 Codesagar.
///DISCLAIMER: Author claims no responsibility for any damages whatsoever
///stemming out either directly or indirectly from the usage of any of the
///source code made available through Codesagar.
#endregion
using System;
///Codesagar factory dishes
namespace Factories
{
///
/// This class provides a generic singleton base class that provides a static member
/// of the derived type tha points to the singleton instance. Ths class also provides
/// the public singleton getter Instance to access the public interface of the derived
/// class.
///
/// The derived class that should be used as a singleton
public class SingletonBase where T : class, new()
{
#region protected static instance
protected static T s_instance;
#endregion
#region protected constructor
protected SingletonBase()
{
}
#endregion
#region Singleton interface
public static T Instance
{
get
{
if (s_instance == null)
{
T instance = new T();
// Insure all writes used to construct new value have been flushed.
// For more, see
// http://blogs.msdn.com/brada/archive/2004/05/12/volatile-and-memorybarrier.aspx
System.Threading.Thread.MemoryBarrier();
s_instance = instance;
}
return s_instance;
}
}
#endregion
}
///
/// A sample singleton class that derives its singleton properties
/// from the Singleton
///
class MySingleton : SingletonBase
{
public void Test()
{
Console.WriteLine("MySingleton::Test called!");
}
}
public class Client
{
public void TestMySingleton()
{
MySingleton.Instance.Test();
}
}
}
Subscribe to:
Posts (Atom)