Skip to main content

Posts

VBA versus .NET

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         {   ...

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()         {         }   ...

Abstract Cat Factory (Amusement)

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...

Adapter or Wrapper

This is code possible through .NET, in that it bridges across to COM for automation in VBA.  This concept can be extended to work for many other COM-based applications.  This code allows the add-in to expose internal .NET-coded procedures to Excel COM, extending the use of the .NET code. Salient Characteristic(s) Handle interface between different, incompatible systems The interface is required because COM is interface-based Other code elements are required for this to work properly Registering the DLL Creating the AdapterForVBA class on startup Code using System; using System.Runtime.InteropServices; namespace DesignPatterns {     /// <summary>     /// Interace to expose VSTO/COM obects for COM and Excel     /// Used by class below     /// </summary>     [ComVisible(true)]     [Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]     [InterfaceType(ComInterfaceType.InterfaceIs...

Facade

This is derived from actual code of mine, and elements of this sample combine the Facade pattern with the Publisher (Observer) pattern.  The primary class, based on the ISubscriber interface, is fairly complicated to use, requiring delegates, threading, and asynchronous callbacks.  The facade, based on ISubscriberFacade, encapsulates all the methods required to work with the Windows Communication Foundation (WCF) service , handling threading, delegate creation, and asynchronous callbacks internally, so that that the clients only need to create the object.  I wrote the encapsulating client to ease the adoption of the WCF service for legacy clients, seeing that the code complexity was likely a hurdle. Salient Characteristic(s) Reduces or hides complexities to other clients or systems Simplified interaction between systems and/or types Note DispatchingObservableCollection is based on ObservableCollection commonly used in Windows Presentation Foun...

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 objects A private constructor Named objects Code 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;              ...

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 constructor Static variable for self-tracking Three (3) variants Eager Lazy Thread-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()         {        ...