Skip to main content

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

Popular posts from this blog

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
       …

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…