This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SingletonExamples | |
{ | |
/// <summary> | |
/// Broken, non thread-save solution. | |
/// Don't use this code. | |
/// </summary> | |
public class SingletonFirst | |
{ | |
private static SingletonFirst _instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SingletonExamples | |
{ | |
/// <summary> | |
/// Simple Thread safe solution. | |
/// </summary> | |
public class SingletonThreadSafe | |
{ | |
private static SingletonThreadSafe _instance; | |
private static readonly object _lock = new object(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SingletonExamples | |
{ | |
/// <summary> | |
/// Using static constructor. | |
/// </summary> | |
public class SingletonStatic | |
{ | |
private static SingletonStatic instance; | |
private SingletonStatic() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace SingletonExamples | |
{ | |
/// <summary> | |
/// Solution using Lazy implementation. | |
/// </summary> | |
public class SingletonLazy | |
{ | |
private static readonly Lazy<SingletonLazy> instance = |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string data = "Item1"; | |
var action1 = new Action(() => { Console.Write("This is one!"); }); | |
var action2 = new Action(() => { Console.Write("This is two!"); }); | |
var action3 = new Action(() => { Console.Write("This is three!"); }); | |
switch (data) | |
{ | |
case "Item1": | |
action1(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Entity | |
{ | |
public string Type { get; set; } | |
public int GetNewValueBasedOnType(int newValue) | |
{ | |
int returnValue; | |
switch (Type) | |
{ | |
case "Type0": |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var entity = new Entity() { Type = "Type1" }; | |
var value = entity.GetNewValueBasedOnType(6); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum EntityType | |
{ | |
Type0 = 0, | |
Type1 = 1, | |
Type2 = 2 | |
} | |
public abstract class Entity | |
{ | |
public abstract int GetNewValue(int newValue); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EntityFactory | |
{ | |
private Dictionary<EntityType, Func<entity>> _entityTypeMapper; | |
public EntityFactory() | |
{ | |
_entityTypeMapper = new Dictionary<entitytype, func<entity="">>(); | |
_entityTypeMapper.Add(EntityType.Type0, () => { return new Type0Entity(); }); | |
_entityTypeMapper.Add(EntityType.Type1, () => { return new Type1Entity(); }); | |
_entityTypeMapper.Add(EntityType.Type2, () => { return new Type2Entity(); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Type0Entity : Entity | |
{ | |
public override int GetNewValue(int newValue) | |
{ | |
return newValue; | |
} | |
} | |
public class Type1Entity : Entity | |
{ |
OlderNewer