The design pattern framework

Introduction

Object-oriented programming has a sort of common architectural solutions to common problems known as design pattern (http://en.wikipedia.org/wiki/Design_pattern).

A design pattern specifies entities (classes) and their relations and aims, and usually is designed using UML http://en.wikipedia.org/wiki/uml.

Examples of them can be "observer" http://en.wikipedia.org/wiki/Observer_pattern or "abstract factory" http://en.wikipedia.org/wiki/Abstract_factory. There are tons of them.

Implementing a design pattern in real-life projects can be hard because not every programmer has the appropiate knowledge to use them, or every one would perform its own "version" based on their experience, knowledge or project costs.

Now Design Pattern Foundation exposes an API which may save time on coding an abstract implementation to any design pattern, giving to them a common specification and a set of features:
  • Instantiation and isolation. A component, application or services based on a pattern over design pattern API can be instantiated supporting isolation.
  • Generic identifier to entities. Any entity within a pattern has a generic identifier, so a logic using it provides the actual typing.
  • LINQ and lambda capabilities. Entities within a pattern are queryable.
  • Common specification to all design patterns. This is every design pattern implementation may share a common specification which may expose a more reusable paradigm.

The API

Software design pattern API basically has seven classes:
  • PatternMember, representing any entity within a design pattern.
  • PatternMemberIdentifier, representing an unique identifier for a PatternMember.
  • PatternInstance, representing an organizative entity which groups and isolates a set of PatternMember.
  • PatternInstanceIdentifier, representng an unique identifier for a PatternInstance.
  • PatternInstanceMember, representing an entity withn a PatternInstance.
  • PatternInstanceMemberIdentifier, representing an unique identifier for a PatterInstanceMember.
  • PatternInstanceManager, representing a collection of PatternInstance.

For API specific documentation you should download the help file on "Downloads" category.

Example

A minimal observer implementation Observer pattern based on Wikipedia.

The type for identifiers' name is System.Guid.

Instance

Instance type for Observer.

In C#
using Comkarl.Architecture.Patterns;

// It is an abstract class too because that fact skips inherited abstract members implementation.
public abstract class Instance : PatternInstance<Guid>
{
    public Instance(PatternInstanceIdentifier<Guid> id)
        : base(id)
    {

    }
}

Observable

Observable is represented by a marker interface.

'''In C#'''
public interface IObservable { }

Observer

Observer is implemented as an abstract class.

Note that identifiers for every instance are auto-generated.

In C#
using Comkarl.Architecture.Patterns;

public abstract class Observer : PatternInstanceMember<Guid, Instance>
{
    public Observer()
        : base(new PatternInstanceMemberIdentifier<Guid>(Guid.NewGuid()))
    {
    }
    
    // Accepts an observable object and an array of arguments.
    public abstract void Update(IObservable observableObject, params object[] args);
}

Subject

In C#
using Comkarl.Architecture.Patterns;

public abstract class Subject : PatternInstanceMember<Guid, Instance>, IObservable
{
    public Subject()
        : base(new PatternInstanceMemberIdentifier<Guid>(Guid.NewGuid()))
    {
    }

    private readonly List<Observer> observerList = new List<Observer>();

    private List<Observer> Observers
    {
        get { return observerList; }
    }

    public virtual void Attach(Observer someObserver)
    {
        Observers.Add(someObserver);
    }

    public virtual void Deattach(Observer someObserver)
    {
        Observers.Remove(someObserver);
    }

    public virtual void Notify()
    {
        foreach (Observer observer in Observers)
        {
            observer.Update(this, "I told you to udpate!");
        }
    }
}

Last edited Feb 11, 2010 at 10:55 AM by MFidemraizer, version 10

Comments

No comments yet.