简体   繁体   English

工厂模式理解

[英]Factory Pattern Understanding

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? 如何确保仅通过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();

        }

Yes, your intuition here is correct; 是的,你的直觉是正确的; if you want to restrict the construction of your CSVDataSourceFactory class then you have your access modifiers wrong. 如果你想限制你的CSVDataSourceFactory类的构造,那么你的访问修饰符是错误的。

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. 您应该将默认构造函数标记为internal以便只有程序集中的其他类可以构造它们。 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";
  }
}

If you want to know how to prevent people from being able to create instances of the datasources you can do it through interfaces. 如果您想知道如何防止人们创建数据源的实例,您可以通过接口来完成。 Create an interface for each datasource with all of the method that you want it to expose. 使用您希望它公开的所有方法为每个数据源创建一个接口。 Make the class itself internal or a private inner class of the factory (internal is usually appropriate) and the interface public. 使类本身内部或工厂的私有内部类(内部通常是合适的)和接口public。

If the question is not how to stop people from creating instances but if you should, that's more of a subjective answer. 如果问题不是如何阻止人们创建实例,但如果你应该这样做,那更多的是一个主观的答案。 There are a few things to consider: 有几点需要考虑:

  1. What are the consequences of someone bypassing the factor and creating their own datasource? 有人绕过因子并创建自己的数据源有什么后果? Nothing bad? 没什么不好? Poorer performance? 表现较差? Major security violation? 重大安全违规? If they're only shooting themselves in the foot or hurting nothing, maybe you don't need to bother to stop them. 如果他们只是在脚下射击或者什么都不伤,也许你不需要费心去阻止他们。
  2. How hard is it to stop them? 阻止他们有多难? Do you need to prevent them from using reflection to access the underlying class? 您是否需要阻止它们使用反射来访问基础类? Is using an interface layer sufficient? 使用的接口层是否足够?
  3. How trustworty are your users? 您的用户如何信任? Is it just an internal app for your own use or for use on your own team? 它只是一个供您自己使用或在您自己的团队中使用的内部应用程序吗? Is it being shipped to thousands of other people? 它被运送给成千上万的人吗? A small internal use app might not bother, a large scale one needs to assume there are either really malicious or ignorant users and what you expose will matter significantly. 一个小的内部使用应用程序可能不会打扰,一个大规模的应用程序需要假设有真正的恶意用户或无知用户,你揭露的内容将显着重要。 It is also important to improve usability in general as the userbase grows. 随着用户群的增长,提高可用性也很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM