Factory Pattern: Determining concrete factory class instantiation?

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-06-05T22:13:45Z Indexed on 2010/06/05 22:22 UTC
Read the original article Hit count: 383

Filed under:
|
|

I'm trying to learn patterns and I'm stuck on determining how or where a Factory Pattern determines what class to instanciate. If I have a Application that calls the factory and sends it, say, an xml config file to determine what type of action to take, where does that logic for interpreting the config file happen?

THE FACTORY

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

namespace myNamespace
{
    public abstract class SourceFactory
    {
        abstract public UploadSource getUploadSource();
    }
    public class TextSourceFactory : SourceFactory
    {
        public override UploadSource getUploadSource()
        {
            return new TextUploadSource();
        }
    }
    public class XmlSourceFacotry : SourceFactory
    {
        public override UploadSource getUploadSource()
        {
            return new XmlUploadSource();
        }
    }
    public class SqlSourceFactory : SourceFactory
    {
        public override UploadSource getUploadSource()
        {
            return new SqlUploadSource();
        }
    }
}

THE CLASSES

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

namespace myNamespace
{
    public abstract class UploadSource
    {
        abstract public void Execute();
    }
    public class TextUploadSource : UploadSource
    {
        public override void Execute()
        {
            Console.WriteLine("You executed a text upload source");
        }
    }
    public class XmlUploadSource : UploadSource
    {
        public override void Execute()
        {
            Console.WriteLine("You executed an XML upload source");
        }
    }
    public class SqlUploadSource : UploadSource
    {
        public override void Execute()
        {
            Console.WriteLine("You executed a SQL upload source");
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about design-patterns