11

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?

user1615362
  • 3,657
  • 9
  • 29
  • 46

4 Answers4

9

"Abstract" in "abstract factory" has nothing to do with "abstract" in abstract class. Abstract factory is "base" for concrete factory, but design pattern itself does not enforce any particular implementation. Abstract factory can be abstract or even concrete class, interface or some form of duck typed object depending on language you use.

Indeed in C# interface is very reasonable way to specify Abstract Factory.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
7

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.

Pieter Geerkens
  • 11,775
  • 2
  • 32
  • 52
4

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.

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • But if I use an interface, doesn't this "break" the original design pattern? I am under an impression that the point of using DP is for maintain a consistency. – user1615362 Sep 17 '13 at 02:43
  • Not as far as I know. In languages like `C++` where multiple inheritance is allowed, one can argue interfaces don't exist. Design Patterns are merely guidelines how to structure code. They don't come with a formal description whether you should use interfaces or abstract classes, if the fields should be private or public... – Willem Van Onsem Sep 17 '13 at 02:49
3

It's probably a typo in the book and the first "interface" was actually an abstract class.

There's no such thing as an "abstract interface". An interface is by definition abstract.

aleciten
  • 114
  • 3