[英]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: 有几点需要考虑:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.