Why does Abstract Factory use abstract class instead of interface?

后端 未结 4 541
故里飘歌
故里飘歌 2021-01-12 02:37

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 questio

4条回答
  •  感动是毒
    2021-01-12 03:21

    An interface is indeed the most elegant way to do this.

    An argument to use an abstract class: Sometimes it can be useful to use an abstract class when the class maintains a state and some parts are already known.

    However it is safer to first use an interface and then optionally implement an abstract class who introduces such state. Since C# only allows single inheritance one could run into trouble when a ConcreteFactory should inherit from different classes.

提交回复
热议问题