You think about it wrong. Regardless of whether you use an interface or an abstract class, you must decide when creating the object. Later, when you decide to build it, you will use the abstract factory template.
They simply showed that they can handle any form of abstraction. An interface is generally considered "better" because it allows for a weaker link without forcing the constructor to not use your inheritance link. However, itβs even less likely to use factory to return an interface, because you usually want to give people the opportunity to implement their own implementation of interfaces, which is traditionally not a model of the factory template (although this certainly can be)
There is one specific advantage of using interfaces that I can think of; you can implement two different interfaces with the same class. Here is an example (pretty dumb):
interface IProvideInfo { string Get(); } interface IProvideConnectionString : IProvideInfo { } interface IProvideCurrentUserName : IProvideInfo { } class CurrentContextProvider : IProvideConnectionString, IProvideCurrentUserName { string IProvideConnectionString.Get() { return ConfigurationManager.ConnectionStrings["db"]; } string IProvideCurrentUserName.Get() { return _currentUserName; } string _currentUserName; public string SetCurrentUserName(string s) { _currerntUserName = s; } } public class InfoProviderFactory { CurrentContextProvider _provider = new CurrentContextProvider() public IProvideInfo GetProvider(string key) { return _provider; } }
source share