Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • What is the major difference between my code an yours? Although it seems that in yours, the super class knows about the subclasses Commented Feb 8, 2013 at 22:21
  • 1
    @MohammadRahmani 1) As it does in yours. 2) Only in the first example, and I clarified afterwards that it was just for demonstration and real scenarios would be different. 3) The factory is the abstract class which sub classes extend. Factory patterns don't make sense if the returned object isn't of the same type as the one creating the objects. Commented Feb 8, 2013 at 22:55
  • But I think you are making the super class know about the sub classes and this is an anti pattern Commented Feb 8, 2013 at 23:29
  • 1
    In the second example, the super class does NOT know of the children. Commented Feb 9, 2013 at 0:03
  • 1
    Hi @MohammadRahmani. I think what might be throwing you off is the way the object is being created. You generally avoid making your superclass dependent on the subclasses. Colin avoided that by using reflection to find the correct subclass to instantiate. The factory pattern doesn't require that the superclass create the instances of subclasses. In fact, the "Abstract Factory Pattern" article on Wikipedia uses a separate class as the factory. That class does not use reflection -- it creates the objects directly. Commented Feb 11, 2013 at 15:16