Skip to main content

Posts

Showing posts from July, 2012

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 objectsA private constructorNamed objectsCode

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))
                {
                    item = new Multiton();

                    //calculate next key
       …

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 constructorStatic variable for self-trackingThree (3) variants
EagerLazyThread-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 == null)
                {
                    lock (_SyncRoot)
                    {
    …

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 InterfaceDeep copies requires use of creation of new objects, not referencesFor this, use of Serializable attribute to create a new copiesCode

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 = value;
            }
        }
    }

    public static class CommonM…

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
        {
            public LazyObjectType Name;
           …

Builder

Below is a fairly straightforward 'fill-in-the-blanks' example of a Builder design pattern:

Salient Characteristic(s)

Defines a base typeDefers creation of type to derived typesCode

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
        public abstract void BuildMethod1();
        public abstract void BuildMetho…