Posts

Showing posts from October, 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 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
    { 
   …