Posts

Migrating and Design Planning

Image
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:

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.

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

Image
Design 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

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 objectExtends 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.  
    /// T…

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 collectionAny modification method of a collection would nbe the same as modifying the containerCode

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

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 independentlyUseful 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
    { 
   …

Abstract Cat Factory (Amusement)

Image
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 typesClass creates derived (concrete) classes based on type requiredCode

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 (_…