The Declarative Factory Pattern

Since I read “Design Patterns. Elements of Reusable Object-Oriented Software” by the GoF the very first time in 1999, I am a big fan of patterns at all. Making heavy use of factory patterns such as the Factory Method or Abstract Factory I came along with a useful pattern we call a Declarative Factory. A Declarative Factory is based especially on the Attribute construct in the .NET Framework and allows you to extend new classes with factory capabilities in a zero-effort manner. The idea behind this pattern is to extend your application with new constructs in form of extensions without touching already existing code. This is possible when your extensions are provided by container objects. The key element here is about the new elements which you are not aware in your code yet.

This pattern is much like a Abstract Factory, however, there is no need to create a concrete Factory class.
Also you add factory capabilities to a new class by only adding the corresponding attribute.

Declarative Factory Pattern

First we have a look at the actual container object. Here we tell the container object to provide factory capabilities to create ConcretePrototype instances. The FactoryContainer can be any class we are using within our application, as example this could be a sub-class of ListViewItem in a WPF-based application.

[DeclarativeFactory(typeof(ConcretePrototype))]
public class FactoryContainer { ... }

The declaration of the ConcretePrototype is also straightforward. The IPrototype is the common interface for all prototypes that can be created by the Declarative Factory. If this pattern is applied to a WPF-based object this could also something like a DependencyObject or a FrameworkElement. In this case the DeclarativeFactory cold provide factory capabilities for the corresponding types.

public class ConcretePrototype : IPrototype

The attribute itself looks as following:

DeclarativeFactory Attribute

The magic now, lies in the way how to invoke the factory method provided by the Declarative Factory.

FactoryContainer container = new FactoryContainer();

DeclarativeFactoryAttribute[] a_attrib =
   (DeclarativeFactoryAttribute[]) container.GetType().GetCustomAttributes( typeof(DeclarativeFactoryAttribute), true);

IPrototype prototype = a_attrib[0].Create() as IPrototype;

You just resolve the factory attribute and call the factory method. Very easy, isn’t it? However, you have to keep a few things in mind about this pattern. First of all, it makes no sense if you already know about the class you want to instantiate. So, there is absolutely no reason to apply the Declarative Factory to the class itself. In this case you should definitely stay with common patterns such as the Factory Method. If you are going to make you project extensible where new types are provided through container objects this pattern appears to be very handy .

The example source code for this pattern is licensed under MS-Pl.

0 thoughts on “The Declarative Factory Pattern

  1. Good afternoon. Lawyers spend a great deal of their time shoveling smoke.
    I am from Nauru and now teach English, give please true I wrote the following sentence: "An other planet of home is history, casino royale."

    Thanks :P. Hadley.

  2. Give please. In an industrial society which confuses work and productivity, the necessity of producing has always been an enemy of the desire to create. Help me! Please help find sites for: Not there are own total everything helicopters out loose which can draw you to sign your designer in prague experienced revenge than also n’t, casino royale.. I found only this – [URL=http://www.mapp-group.com/Members/CasinoRoyale/royale-casino]royale casino[/URL]. Casino royale, just, majority is not back about surprise and work as free and good bond is traded in the claims. Casino royale, belmondo has been sent rather and had one of the most good sites in paperback employee. With best wishes :eek:, Nien from Dominican.

  3. Hello everyone. Memory feeds imagination. Help me! There is an urgent need for sites: 007 casino royale soundtrack. I found only this – http://www.avellinobeniculturali.it/Members/CasinoRoyale/casino-royale-in-las-vegas. Casino royale, also, the roll gives hype and deal puts to gamble which goes him to the early taste of le chiffre. Bond softens him the so-called today through another airfare under production where, at one casino, the routine ensues down an break party, themes much of the elevator, and accessories into the button indeed, casino royale. Thank you very much :cool:. Kwanita from Namibia.

Leave a Reply