Top

Tags


Roadkill .NET Wiki

Google ads

Recommended reading


Search

Friday
Jan022009

C# Design Patterns: the Abstract Factory Pattern

Summary

The abstract factory is the base class of all factory classes, but is responsible for creating instances of the classes that are derived from it (via a static method).

It goes one step further after this, so each derived class creates an instance of another abstract class (Renderer in my example) specialized for its use.

Example usage

As the code below illustrates, a common use is for abstracting GUI elements out, so that that each different concrete factory creates components for a different platform, this could be an operating system or device like the web/windows. This is a fairly common example that's found in the Oreilly C# book and on wikipedia.

namespace DesignPatterns
{

    public abstract class AbstractPluginFactory
    {
        public static AbstractPluginFactory GetFactory(string renderer)
        {
            switch (renderer)
            {
                case "ASPX":
                    return new ASPXRendererFactory();
                case "Winforms":
                    return new WinformsRendererFactory();
                default:
                    throw new NotSupportedException(string.Format("{0} is not supported", renderer));
            }
        }

        public abstract Renderer Renderer { get; } 
    }

    public class ASPXRendererFactory : AbstractPluginFactory
    {
        private Renderer _renderer;

        public override Renderer Renderer
        {
            get
            {
                return _renderer;
            }
        }

        public ASPXRendererFactory()
        {
            // This could be a protected field in PluginFactory
            _renderer = new ASPXRenderer();
        }
    }

    public class WinformsRendererFactory : AbstractPluginFactory
    {
        private Renderer _renderer;

        public override Renderer Renderer
        {
            get
            {
                return _renderer;
            }
        }

        public WinformsRendererFactory()
        {
            // This could be a protected field in PluginFactory
            _renderer = new WinformsRenderer();
        }
    }

    public abstract class Renderer
    {
        public abstract void DrawImage(string filename);
    }

    public class WinformsRenderer : Renderer
    {
        public override void DrawImage(string filename)
        {
            // This would draw onto a canvas provided via
            // a Graphics object.
            Console.WriteLine("Drawing an image for Windows forms");
        }
    }

    public class ASPXRenderer : Renderer
    {
        public override void DrawImage(string filename)
        {
            // This would output an <img..> tag to the child controls or similar.
            Console.WriteLine("Drawing an image for ASP.NET");
        }
    }
}

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
« C# Design Patterns: the Singleton pattern | Main | C# Design Patterns: the Factory pattern »