工厂模式问题
我第一次尝试使用工厂方法。似乎有两种方法可以做到这一点,但我不确定何时使用哪种(或者其中一种方法是否错误?)。
1)创建一个抽象类,它有一个被子类覆盖的方法:
public abstract class EmployeeCreator
{
public abstract Employee FactoryMethod();
}
public class DeveloperCreator : EmployeeCreator
{
public override Employee FactoryMethod()
{
return new Developer();
}
}
2)创建一个包含一个工厂方法的类:
public class EmployeeFactory
{
public static Employee CreateEmployee(EmployeeType type)
{
if (type == EmployeeType.Developer)
return new Developer();
else
return new Secretary();
}
}
在我的例子中,#2 似乎会容易得多,因为我不必创建所有这些“创建者”子类,除了返回正确的员工类型之外,它们实际上没有做太多事情。
但它仍然使用工厂模式吗?
I'm taking a stab at using the factory method for the first time. It seems like there are 2 ways to do it and I'm not sure when to use which (or if one of them is wrong?).
1) Create an abstract class that has a method that is overridden by subclasses:
public abstract class EmployeeCreator
{
public abstract Employee FactoryMethod();
}
public class DeveloperCreator : EmployeeCreator
{
public override Employee FactoryMethod()
{
return new Developer();
}
}
2) Create a class with one factory method in it:
public class EmployeeFactory
{
public static Employee CreateEmployee(EmployeeType type)
{
if (type == EmployeeType.Developer)
return new Developer();
else
return new Secretary();
}
}
It seems like in my case #2 is going to be a lot easier because I won't have to create all of these "creator" subclasses that really don't do much other than return the right type of the employee.
But is it still using a factory pattern?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的第一个案例是 Abstract Factory 模式,而第二个案例是 工厂方法。这是两种不同的模式,都是有效且有用的。 (实际上并没有“工厂模式”——每当您听到这个术语时,它都是对上面提到的模式之一的引用。)
如果您只需要创建一个产品,那么工厂方法通常是更好的选择。如果您(有机会)需要创建一系列相关产品,那么您需要抽象工厂。如果您需要将工厂注入代码中的不同位置,后者也更好,因为为此目的最好有一个不同的接口。
Your first case is the Abstract Factory pattern, while the 2nd is Factory Method. These are two different patterns, both are valid and useful. (There is no "Factory pattern" actually - whenever you hear this term, it is a reference to one of the patterns mentioned above.)
If you only have a single product to create, Factory Method is usually a better choice. If you have (any chance of) the need to create a family of related products, you need Abstract Factory. The latter is also better if you need to inject your factory to different places in your code, as it is better to have a distinct interface for this purpose.
老实说,我不确定哪种方式是“正确的”,但我可以告诉你,正如你所说,你的第二个选择在你的场景中会更有用。如果可以的话,我尝试在抽象基类中使用静态创建方法,以便根据必要的逻辑实例化正确的子类。听起来这也是你正在尝试做的事情,我认为这没有任何问题。
请记住,设计模式是作为解决问题的指南并通过标准化来帮助提高可维护性。如果你发现某种东西保持了相同的“精神”,但出于某种明显的原因更适合你的需求,我认为没有理由不去追求它。
I'm honestly not sure which way is "right," but I can tell you that your second option is going to be much more useful in your scenario, as you have stated. I try to use a static creation method in the abstract base class when I can so that the right subclass is instantiated based on the necessary logic. It sounds like that's what you are trying to do also, and I don't see any problem with that.
Remember that the design patterns are there as a guide to solve problems and to help maintainability through standardization. If you find something that maintains the same "spirit" but suits your needs better for some discernible reason, I see no reason not to go for it.