Wednesday, November 25, 2015

Migrating and Design Planning

I have been toying with the idea of migrating one of my sites to a better host - it was supported by Yahoo and now AAbaco - and implementing some newer technologies. Among products I have used at work or are working with peripherally, I am considering using ASP.NET MVC, Entity Framework, ReSTful API's, NoSQL, and Azure-hosted databases - it is currently a mixture of very low-end PHP, HTML5/CSS3, light Javascript, and MySQL - so I decided to write up an architectural diagram - it looks like any standard architecture, with maybe a few additional elements - to help with the planning:

Preliminary architectural diagram of planned site.

Thursday, March 19, 2015

Singleton (Update)


I recently came across a new way of implementing the Singleton pattern, detailed in this article. The article details, (1) the use of the volatile keyword, and (2) a new simpler way of implementing a Singleton by using the Lazy<T> class, something that was not available when I first wrote the section on the Singleton.

Sunday, November 16, 2014

Review - Design Patterns: Elements of Reusable Object-Oriented Software

Design Patterns: Elements of Reusable Object-Oriented SoftwareDesign Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma
My rating: 5 of 5 stars

Depending on on how you think of programming, this book could be incredibly insightful, or horribly abstract and impractical. Since I prefer and tend to think in patterns and abstractions, I found this book close to my heart. It uses a variety of languages for examples, so a willingness to explore concepts, not practical solutions, is essential.

View all my reviews

Sunday, November 18, 2012

Decorator Pattern

This is an example of the Decorator pattern, in this case a decorator for ObservableCollection.  Working with WPF and ObservableCollection using threads, one will run into the problem whereby the ObservableCollection cannot be updated from outside the owning thread; Delegates and Invoke will not work. A solution is to contain and expand the class, as is done in this example on michIG's Blog.

This linked file contains the original code in C#, as well as the same code converted to VB.NET.

Salient Characteristic(s)
  • Sets an internal pointer to the decorated object, sending method calls and property actions to the internal object
  • Extends the object by wrapping it and adding some aspect handled by the decorator

Code

using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.Collections.Specialized;
using System.ComponentModel;

namespace DesignPatterns
{
    /// <summary>  
    /// This class is an observablecollection which invokes automatically.  
    /// This means that any change will be done in the right thread.  
    /// </summary>  
    /// <typeparam name="T">The type of the elements</typeparam>  
    public class DispatchingObservableCollection<T> : ObservableCollection<T>
    {
        /// <summary>  
        /// The default constructor of the ObservableCollection  
        /// </summary>  
        public DispatchingObservableCollection()
        {
            //Assign the current Dispatcher (owner of the collection)  
            _currentDispatcher = Dispatcher.CurrentDispatcher;
        }

        private readonly Dispatcher _currentDispatcher;


        /// <summary>  

        /// Executes this action in the right thread  
        /// </summary>  
        ///<param name="action">The action which should be executed</param>  
        private void DoDispatchedAction(Action action)
        {
            if (_currentDispatcher.CheckAccess())
                action.Invoke();
            else
                _currentDispatcher.Invoke(DispatcherPriority.DataBind, action);
        }

        /// <summary>  

        /// Clears all items  
        /// </summary>  
        protected override void ClearItems()
        {
            DoDispatchedAction(BaseClearItems);
        }

        private void BaseClearItems()

        {
            base.ClearItems();
        }

        /// <summary>  

        /// Inserts a item at the specified index  
        /// </summary>  
        ///<param name="index">The index where the item should be inserted</param>  
        ///<param name="item">The item which should be inserted</param>  
        protected override void InsertItem(int index, T item)
        {
            DoDispatchedAction(() => BaseInsertItem(index, item));
        }

        private void BaseInsertItem(int index, T item)

        {
            base.InsertItem(index, item);
        }

        /// <summary>  

        /// Moves an item from one index to another  
        /// </summary>  
        ///<param name="oldIndex">The index of the item which should be moved</param>  
        ///<param name="newIndex">The index where the item should be moved</param>  
        protected override void MoveItem(int oldIndex, int newIndex)
        {
            DoDispatchedAction(() => BaseMoveItem(oldIndex, newIndex));
        }

        private void BaseMoveItem(int oldIndex, int newIndex)

        {
            base.MoveItem(oldIndex, newIndex);
        }

        /// <summary>  

        /// Removes the item at the specified index  
        /// </summary>  
        ///<param name="index">The index of the item which should be removed</param>  
        protected override void RemoveItem(int index)
        {
            DoDispatchedAction(() => BaseRemoveItem(index));
        }

        private void BaseRemoveItem(int index)

        {
            base.RemoveItem(index);
        }

        /// <summary>  

        /// Sets the item at the specified index  
        /// </summary>  
        ///<param name="index">The index which should be set</param>  
        ///<param name="item">The new item</param>  
        protected override void SetItem(int index, T item)
        {
            DoDispatchedAction(() => BaseSetItem(index, item));
        }

        private void BaseSetItem(int index, T item)

        {
            base.SetItem(index, item);
        }
        
        /// <summary>  
        /// Fires the CollectionChanged Event  
        /// </summary>  
        ///<param name="e">The additional arguments of the event</param>  
        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            DoDispatchedAction(() => BaseOnCollectionChanged(e));
        }

        private void BaseOnCollectionChanged(NotifyCollectionChangedEventArgs e)

        {
            base.OnCollectionChanged(e);
        }

        /// <summary>  

        /// Fires the PropertyChanged Event  
        /// </summary>  
        ///<param name="e">The additional arguments of the event</param>  
        protected override void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            DoDispatchedAction(() => BaseOnPropertyChanged(e));
        }

        private void BaseOnPropertyChanged(PropertyChangedEventArgs e)

        {
            base.OnPropertyChanged(e);
        }
    }
}


Monday, October 29, 2012

Composite Pattern

A implementation of the composite pattern similar to a class that would be used for constructing a binary search tree.  Better, more detailed implementations are known as red-black or AVL trees.

Salient Characteristic(s)

  • Reduces complexity by treating objects consistently, as opposed to having different methods and properties for each collection
  • Any modification method of a collection would nbe the same as modifying the container
Code

namespace DesignPatterns
{
    /// <summary>
    /// An example of composite that might be used in a binary tree
    /// In this case, the tree node is composed of tree nodes
    /// Any modification method of a node, would be the same as modifying the node itself
    /// </summary>
    public class TreeNode 
    {
        private int _Value;
        public int Value
        {
            get { return _Value; }
            set { _Value = value; }
        }

        private TreeNode _Parent;
        public TreeNode Parent
        {
            get { return _Parent; }
            set { _Parent = value; }
        }
    
        private TreeNode _LeftNode;
        public TreeNode LeftNode
        {
            get { return _LeftNode; }
            set { _LeftNode = value; }
        }
    
        private TreeNode _RightNode;
        public TreeNode RightNode
        {
            get { return _RightNode; }
            set { _RightNode = value; }
        }

        public void Add(TreeNode node)
        {
            //stub for method to add node correctly
            //would also manage and rearrange its own nodes
        }

        public void Remove(TreeNode node)
        {
            //stub for removing node
            //also responsible for managing and rearranging nodes when removing
        }
    
        public TreeNode(int value)
        {
            _Value = value;
        }
    }
}

Wednesday, October 3, 2012

Bridge Pattern

A bare-bones, generic implementation of the bridge pattern, using inheritance, polymorphism, and abstraction. 

Salient Characteristic(s)

  • Decouple classes, allowing them to vary independently
  • Useful when frequent changes are made to classes

Code

namespace DesignPatterns

{
    /// <summary>
    /// The implementor: the abstract class, and concrete implementation of one side of the relation
    /// </summary> 
    public interface IBridgeAbstraction
    {
        void Build();
    }
    
    abstract class BridgeAbstraction : IBridgeAbstraction
    {
        public abstract void Build();
    }

    class ConcreteBridge1 : BridgeAbstraction
    {
        public override void Build()
        {
        }
    }

    class ConcreteBridge2 : BridgeAbstraction
    {
        public override void Build()
        {
        }
    }
    
    
    /// <summary>
    /// The abstract class, and concrete implementation of other side of the relations 
    /// </summary>
    public abstract class AbstractionBridgeSupplier
    { 
        public abstract void BuildBridge(IBridgeAbstraction bridge);
    }

    public class ConcreteBridgeSupplier : AbstractionBridgeSupplier
    {
        public override void BuildBridge(IBridgeAbstraction bridge)
        {
            bridge.Build();
        }
    }


    /// <summary>
    /// The client:  the intermediary between the two independently varying sides 
    /// </summary>
    public class ConcreteBridgeBuilder
    {
        public ConcreteBridgeBuilder()
        {
            ConcreteBridgeSupplier supplier = new ConcreteBridgeSupplier();
            supplier.BuildBridge(new ConcreteBridge1());
            supplier.BuildBridge(new ConcreteBridge2());
        }
    }
}

Wednesday, August 15, 2012

Abstract Cat Factory (Amusemet)

A friend amusingly posted on Facebook a retro photograph of four (4) 'designers' around a cat, with the caption alluding to designing hats for cats, and so I decided to make, somewhat incongruously, an abstract cat factory, which varies by the location of the cat..  

Salient Characteristic(s)
  • Classes derived from abstract types
  • Class creates derived (concrete) classes based on type required
Code

namespace DesignPatterns
{
    public enum Location
    {
        Kitchen,
        Bedroom,
        LivingRoom
    }

    public enum Amusements
    {
        String,
        Mouse,
        Food,
        Sleep
    }
    
    public abstract class Feline
    {
        public abstract Feline HereKittyKitty();
    }

    public class Cat : Feline
    {
        private Location _Location;
        private Feline _Cat = null;
        private Amusements _Amusement;
        public Cat(Location location)
        {
            _Location = location;
        }

        public override Feline HereKittyKitty()
        {
            switch (_Location)
            { 
                case Location.Bedroom:
                    _Amusement = Amusements.Sleep;
                    break;
                case Location.Kitchen:
                    _Amusement = Amusements.Food;
                    break;
                case Location.LivingRoom:
                    _Amusement = Amusements.String;
                    break;
                default:
                    _Amusement = Amusements.Mouse;
                    break;
            }

            _Cat = new Cat(_Amusement);

            return _Cat;
        }

        private Cat(Amusements fun)
        { 
        
        }
    }
}