Skip to main content

Posts

Showing posts from July, 2012

VBA versus .NET

Multiton

A fairly simple example of the multiton pattern, with a private constructor, and a tracker for created objects, the name being an integer identifier. Salient Chacteristic(s) A private keyed list for tracking objects A private constructor Named objects Code using System.Collections.Generic; using System.Linq; namespace DesignPatterns {     public class Multiton     {         //read-only dictionary to track multitons         private static IDictionary<int, Multiton> _Tracker = new Dictionary<int, Multiton> { };         private Multiton()         {         }         public static Multiton GetInstance(int key)         {             //value to return             Multiton item = null;                          //lock collection to prevent changes during operation             lock (_Tracker)             {                  //if value not found, create and add                 if(!_Tracker.TryGetValue(key, out item))                 {                

Singleton

The Singleton design pattern seems generally frowned upon, except for use in logging classes.  Below are several variants of the Singleton design pattern, of which, only the thread-safe version merits attention: Salient Chacteristic(s) A private constructor Static variable for self-tracking Three (3) variants Eager Lazy Thread-safe (Double-checked Locking) Code namespace DesignPatterns {     //double-checked locking singleton     //required for threaded environments     public class DoubleCheckingSingleton     {         private static volatile DoubleCheckingSingleton _Instance = null;         private static object _SyncRoot = new object();              //Private constructor prevents instantiation from other classes         private DoubleCheckingSingleton()         {             //class contruction         }                  public static DoubleCheckingSingleton Instance         {             get             {                 if (_Instance ==

Prototype

Whereas ICloneable will be used to return a shallow copy, the data object's attribute Serializable enables creation of a deep copy of the data object, instead of a reference: Salient Chacteristic(s) Shallow copies use the native IConeable Interface Deep copies requires use of creation of new objects, not references For this, use of Serializable attribute to create a new copies Code using System; using System.IO; using Serialization = System.Runtime.Serialization; namespace DesignPatterns {     /// <summary>     /// A serializable data object, the attribute     ///  necessary for simple form of deep copy     /// </summary>     [Serializable]     public class SerializableDataObject      {         public SerializableDataObject(int objectId)         {         }         private int _Id;         public int Id         {             get             {                 return _Id;             }             set             {                 _Id =

Lazy Initialization

A fairly straightforward 'fill-in-the-blanks' example of a Lazy Initialization design pattern, except that this uses an enumeration for the type - I hate passing strings, so easy to screw up - as well as a struct for the type that is lazily initialized: Code using System; using System.Collections.Generic; namespace DesignPatterns {     public class LazyFactoryObject     {         //internal collection of items         //IDictionaery makes sure they are unique         private IDictionary<LazyObjectType, LazyObject> _LazyObjectList =              new Dictionary<LazyObjectType, LazyObject>();         //enum for passing name of type required         //avoids passing strings and is part of type ahead         public enum LazyObjectType         {              None,             Small,             Big,             Bigger,             Huge         }         //standard type of object that will be constructed         public struct LazyObject      

Builder

Below is a fairly straightforward 'fill-in-the-blanks' example of a Builder design pattern: Salient Characteristic(s) Defines a base type Defers creation of type to derived types Code using System; namespace DesignPatterns {     //the type of object to be built by the specific builders     public class BuilderObject     {         public string Property1 = string.Empty;         public string Property2 = string.Empty;         public string Property3 = string.Empty;     }     abstract class Builder     {         //protected object to be built         protected BuilderObject _Object;         //method to return object         public BuilderObject GetBuilder()         {             return _Object;         }         //method to create object         public void BuildObject()         {             _Object = new BuilderObject();         }         //public abstract method(s) for building object         //derived classes will implement         publ