RSS Feed for This PostCurrent Article

Design Pattern in .NET 101 - Factory Method Pattern (Creational Pattern)

Download Sample Code

Factory Method Pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. It lets a class defer instantiation to subclasses.

E.g., I defined a abstract News class.

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

namespace FactoryPattern
{
    abstract class News
    {
    }
}

Different kinds of news are defined..

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

namespace FactoryPattern
{
    class AdNews:News
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FashionNews:News
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FinanceNews:News
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class SportsNews:News
    {
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class WorldNews:News
    {
    }
}

I defined the abstract factory class, Publication

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

namespace FactoryPattern
{
    abstract class Publication
    {
        private ArrayList publishedNews = new ArrayList();

        // Constructor calls abstract Factory method
        public Publication()
        {
            this.PublishNews();
        }

        public ArrayList PublishedNews
        {
            get
            {
                return publishedNews;
            }
        }

        // Factory Method
        public abstract void PublishNews();
    }
}

In the Publication class, the factory method is called, but it is not implemented yet.

Newspaper and FashionMagazine extends Publication class.

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

namespace FactoryPattern
{
    class NewsPaper:Publication
    {
        public override void PublishNews()
        {
            this.PublishedNews.Add(new AdNews());
            this.PublishedNews.Add(new FashionNews());
            this.PublishedNews.Add(new WorldNews());
            this.PublishedNews.Add(new SportsNews());
            this.PublishedNews.Add(new FinanceNews());
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace FactoryPattern
{
    class FashionMagazine:Publication
    {
        public override void PublishNews()
        {
            this.PublishedNews.Add(new AdNews());
            this.PublishedNews.Add(new FashionNews());
        }
    }
}

To test it

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

namespace FactoryPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Publication newsPaper = new NewsPaper();
            Console.WriteLine(newsPaper.GetType().Name);
            foreach (News news in newsPaper.PublishedNews)
            {
                Console.WriteLine("-" + news.GetType().Name);
            }

            Console.WriteLine();

            Publication fashionMagazine = new FashionMagazine();
            Console.WriteLine(fashionMagazine.GetType().Name);
            foreach (News news in fashionMagazine.PublishedNews)
            {
                Console.WriteLine("-" + news.GetType().Name);
            }

            Console.Read();
        }
    }
}

The output

NewsPaper
-AdNews
-FashionNews
-WorldNews
-SportsNews
-FinanceNews

FashionMagazine
-AdNews
-FashionNews


Trackback URL


RSS Feed for This PostPost a Comment