Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Factory Pattern Understanding

Tags:

c#

I have implementated the Factory Pattern as below.

However, since the individual classes are public, nothing prevents someone from instantiating them directly.

Is this correct? How do I ensure that the concrete classes are only created via the Factory?

namespace MRS.Framework
{
    public  abstract class DataSource
    {
        public override string ToString()
        {
            return "DataSource";
        }
    }

    public class XMLDataSource : DataSource
    {

    }

    public class SqlDataSource : DataSource
    {

    }

    public class CSVDataSource : DataSource
    {
        public int MyProperty { get; set; }


        public override string ToString()
        {
            return "CSVDataSource";
        }
    }
}

Factory implementation

namespace MRS.Framework
{
    public abstract class DataSourceFactory
    {
        public abstract DataSource CreateDataSource(DataSourceType datasourcetype);
    }

    public class CSVDataSourceFactory : DataSourceFactory
    {
        public CSVDataSourceFactory()
        {

        }
        public override DataSource CreateDataSource(DataSourceType datasourcetype)
        {
            return new CSVDataSource();
        }
    }


    public class XMLDataSourceFactory : DataSourceFactory
    {
        public override DataSource CreateDataSource(DataSourceType datasourcetype)
        {
            return new XMLDataSource();
        }
    }

    public class SqlDataSourceFactory : DataSourceFactory
    {
        public override DataSource CreateDataSource(DataSourceType datasourcetype)
        {
            return new SqlDataSource();
        }
    }

}

Main

 static void Main(string[] args)
        {
            DataSourceFactory datasourcefactory = new CSVDataSourceFactory();
            CSVDataSource ds = (CSVDataSource)datasourcefactory.CreateDataSource(DataSourceType.CSVDataSource);
            CSVDataSource myds = new CSVDataSource();
            Console.WriteLine(ds.ToString());
            Console.WriteLine(myds.ToString());
            Console.ReadLine();

        }
like image 528
sameer Avatar asked May 08 '12 14:05

sameer


People also ask

Can you explain factory pattern?

A Factory Pattern or Factory Method Pattern says that just define an interface or abstract class for creating an object but let the subclasses decide which class to instantiate. In other words, subclasses are responsible to create the instance of the class.

What is factory pattern and how it is useful?

Factory Method Pattern allows the sub-classes to choose the type of objects to create. It promotes the loose-coupling by eliminating the need to bind application-specific classes into the code.

What are the types of factory pattern?

the abstract factory pattern,the static factory method, the simple factory (also called factory).

What problem does factory pattern solve?

The factory pattern aims to solve a fundamental problem in instantiation – i.e., the creation of a concrete object of a class – in object-oriented programming. In principle, creating an object directly within the class that needs or should use this object is possible, but very inflexible.


1 Answers

Yes, your intuition here is correct; if you want to restrict the construction of your CSVDataSourceFactory class then you have your access modifiers wrong.

However, it's not the access modifier of the class you need to fix, it's the access modifier of the constructor. You should mark the default constructor internal so that only other classes within your assembly can construct them. You will, of course, have to enforce your own rules within that assembly but since you have complete control over that code, it shouldn't be an issue.

public class XMLDataSource : DataSource
{
  internal XMLDataSource() { }
}

public class SqlDataSource : DataSource
{
  internal SqlDataSource() { }
}

public class CSVDataSource : DataSource
{
  public int MyProperty { get; set; }

  internal CSVDataSource() { }

  public override string ToString()
  {
      return "CSVDataSource";
  }
}
like image 197
Michael Edenfield Avatar answered Sep 19 '22 21:09

Michael Edenfield