Home > Development > Abstract Factory

Abstract Factory

September 10, 2010 Leave a comment Go to comments

Definition

Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

Pattern Image

using System;

namespace AbstractFactorySamples
{

    public abstract class Template
    {
        private object Data{ get; set;}

        public abstract string GetFormattedOutput();
    }

    public abstract class TemplateProvider
    {
        public abstract Template GetTextTemplate();
        public abstract Template GetRichContentTemplate();
    }

    public class WeeklyNewsletter : TemplateProvider
    {
        public override Template GetTextTemplate()
        {
            return new WeeklyTextTemplate();
        }

        public override Template GetRichContentTemplate()
        {
            return new WeeklyRichContentTemplate();
        }
    }

    public class WeeklyTextTemplate : Template
    {

        public override string GetFormattedOutput()
        {
            return "Weekly Text";
        }
    }

    public class WeeklyRichContentTemplate : Template
    {

        public override string GetFormattedOutput()
        {
            return "Weekly Rich Content";
        }
    }

    public class MonthlyNewsletter : TemplateProvider
    {
        public override Template GetTextTemplate()
        {
            return new MonthlyTextTemplate();
        }

        public override Template GetRichContentTemplate()
        {
            return new MonthlyRichContentTemplate();
        }
    }

    public class MonthlyTextTemplate : Template
    {

        public override string GetFormattedOutput()
        {
            return "Monthly Text";
        }
    }

    public class MonthlyRichContentTemplate : Template
    {

        public override string GetFormattedOutput()
        {
            return "Monthly Rich Content";
        }
    }

    public enum ContentFrequency
    {
        Weekly,
        Monthly
    }

    public static class TemplateProviderFactory
    {
        public static TemplateProvider GetProvider( ContentFrequency frequency)
        {
            if(frequency == ContentFrequency.Weekly)
            {
                return new WeeklyNewsletter();
            }

            return new MonthlyNewsletter();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            TemplateProvider p = TemplateProviderFactory.GetProvider(ContentFrequency.Weekly);
            Console.Write(p.GetTextTemplate().GetFormattedOutput());

            Console.ReadKey();

        }
    }
}

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: