Factory Understanding Principles

I implemented the Factory pattern as shown below.

However, since individual classes are publicly available, nothing prevents anyone from directly creating them.

It is right? How to ensure that specific classes are created only through 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();
        }
    }

}

home

 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();

        }
+5
source share
2 answers

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

, , . internal, . , , , , .

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";
  }
}
+6

, , . , . factory ( ) .

, , , . , :

  • , - ? ? ? ? , , , .
  • ? , ? ?
  • ? ? ? , , , , , , , . .
+2

All Articles