RSS Feed for This PostCurrent Article

Design Pattern in .NET 101 – Abstract Factory Pattern (Creational Pattern)

Download Sample Code

Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

E.g. I have a DocumentFactory class that either returns DefaultDocumentFactory or CustomDocumentFactory dependening on the environment variable.

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    abstract class DocumentFactory
    {
        public static DocumentFactory GetFactory()
        {
            string documentType = 
             System.Environment.GetEnvironmentVariable("DOC_TYPE");
            if (documentType == null || documentType == "")
            {
                return new DefaultDocumentFactory();
            }
            else
            {
                return new CustomDocumentFactory();
            }
        }

        public abstract Document CreateDocument();
    }
}

DefaultDocumentFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class DefaultDocumentFactory: DocumentFactory 
    {
        public override Document CreateDocument()
        {
            return new DefaultDocument();
        }
    }
}

CustomDocumentFactory

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class CustomDocumentFactory: DocumentFactory 
    {
        public override Document CreateDocument()
        {
            return new CustomDocument();
        }
    }
}

Document, DefaultDocument and CustomDocument are defined.

using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    abstract class Document
    {     
      public string content;

        public String Content
        {
            get
            {
                return this.content;
            }
            set
            {
                this.content = value;
            }

        }

        public void Print()
        {
            Console.WriteLine(content);
        }
       
        public abstract void Write();
        
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class DefaultDocument: Document
    {
        public override void Write()
        {
            this.Content = "Default Document";
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace AbstractFactory
{
    class CustomDocument:Document 
    {
        public override void Write()
        {
            this.Content = "Custom Document";
        }
    }
}

To test it,

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace AbstractFactory
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            DocumentFactory  factory = DocumentFactory.GetFactory();
            Document document = factory.CreateDocument();
            document.Write();
            document.Print();
            Console.Read();
        }
    }
}

The output

Default Document


Trackback URL


Sorry, comments for this entry are closed at this time.