
This inheritance is found in ADO.Net. Can we say that here both the design patterns are being used, Abstract Factory and Factory method?
Abstract Factory: “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”
So here DbProviderFactory is an interface for creating families of related objects i.e. DbConnection, DbCommand which are all related to some database provider like Sql or Oracle provider.
Factory Method: “Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”
So here the line (DbConnection CreateConnection()) is factory method as it defines the interface for creating objects, but let subclasses like SqlProviderFactory and OracleProviderFactory decide what instance to create. Sql provider will create SqlConnection and Oracle provider will create OracleConnection and these types inherit from DbConnection.
Can we say that each method in class DbProviderFactory is a factory method and the pattern as a whole is abstract factory?
Yes, DbProviderFactory is a good example of Abstract Factory pattern. And each CreateXXX method of this factory is a FactoryMethod.