3

In C#... factory design pattern, why all "factory class" should be inherited from "abstract factory class" ??

Without inheriting also we can achieve same functionality. Cant we?

Zodiac
  • 45
  • 1
  • 4
  • 1
    possible duplicate of http://stackoverflow.com/questions/1001767/what-is-the-basic-difference-between-factory-and-abstract-factory-patterns – Mohnkuchenzentrale Nov 10 '12 at 13:15
  • you need to compare abstract factory http://www.dofactory.com/Patterns/PatternAbstract.aspx to factory http://www.dofactory.com/Patterns/PatternFactory.aspx, and see which of them best suits your needs – Mihai Nov 10 '12 at 13:17
  • Thank Kroax, i will check that link. Thanks Mihai will check those links. Please if you can guys check my reply to below answer and confrim my understanding of abstract factory method is correct, then i will really appreciate it. Thanks guys! – Zodiac Nov 10 '12 at 13:25
  • If you want to be able to swap out one type of factory for another at run-time, giving them a common base class makes that very easy. – Mike Jun 07 '19 at 17:20

1 Answers1

4

Factories that inherit from an abstract factory class are doing so to follow the abstract factory pattern. The abstract factory pattern is useful when you want to create a strategy out of whole groups of object types. For this reason it is also called the "kit" pattern.

Often, factories are perfectly viable without an abstraction. If you don't need one now, leave it alone. You can always make an abstract factory later, if it is needed.

Some more reading:

http://en.wikipedia.org/wiki/Abstract_factory_pattern

tallseth
  • 3,635
  • 1
  • 23
  • 24
  • Tallseth - Thanks for your reply. Please correct me if i am wrong, but if i have a abstract class, a abstract method whose return type is a interface. Now i inherit this abstract class and provide implentation for the abstract method by returning new instance of a some class which inherits return type interface. Then in my client code create abstarct class object and invoke the method. Is this "Factory Design pattern" ???? – Zodiac Nov 10 '12 at 13:24
  • That sounds like the Abstract Factory pattern. But it's more important to know that it solves your problem than it is to know what pattern it is. :) – tallseth Nov 11 '12 at 15:37