为什么抽象工厂使用抽象类而不是接口? [英] Why does Abstract Factory use abstract class instead of interface?
问题描述
我学习设计模式,并在书中的第一个例子是关于抽象工厂。
我已经建立了演习VS和所有看起来不错,但有一个问题,我想知道。
I am learning about design patterns and the first example in the book is about Abstract Factory. I have built the exercise in VS and all looks good, but there is one question that I wonder about.
在书中的工厂类等来实现这样的:
In the book the factory class is implemented like this:
public abstract class AbstractVehicleFactory
{
public abstract IBody CreateBody();
public abstract IChassis CreateChassis();
public abstract IGlassware CreateGlassware();
}
完成我已注意到,上述类可以用此来代替的练习后代码:
After completing the exercise I have noted that the above class can be replaced with this code:
public interface IAbstractVehicleFactory
{
IBody CreateBody();
IChassis CreateChassis();
IGlassware CreateGlassware();
}
当然这两个例子的功能完全一样,但我不知道是什么原因用一个抽象类,而不是一个接口?
Of course both examples function exactly the same, but I wonder what is the reason for using an abstract class and not an interface?
推荐答案
这是抽象类能,谨慎,可以在一个非中断扩展方式; 。一个接口的所有更改重大更改
An abstract class can, with care, be extended in a non-breaking manner; all changes to an interface are breaking changes.
更新:结果
相反,接口可以是<强>在或退出类型参数和抽象类不能。有时,一个或另一个更适合于给定的设计,有时则是胜负难料。
Update:
In contrast, an interface can be an in or out type-parameter and an abstract class cannot. Sometimes one or the other is more appropriate for a given design, and sometimes it is a toss-up.
这篇关于为什么抽象工厂使用抽象类而不是接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!