3

A theoretical question. I'm reading Gof's Design Patterns, section Abstract Factory. The book mentions the possibility of implementing this pattern like a Prototype or, if the language permits it, with a Prototype which stores classes instead of objects.

I have understood this; e.g. in Java or Smalltalk, classes are also objects (in Java they are in fact instances of the class Class). Hence, we can store them inside a class and, when needed, invoke the creation of instances of these classes.

In C++, classes are not first class objects; hence, we shouldn't be able to follow this approach. However, couldn't we declare nested classes inside inside a Concrete Factory, with methods which invoke their constructors (and return their instances)? The final result would be the same of other languages such as Java or Smalltalk. Am I right?

Thank you for your attention.

Pippo
  • 1,543
  • 3
  • 21
  • 40
  • 1
    There's an actual [_example_ of this](http://en.wikipedia.org/wiki/Abstract_factory_pattern#C.2B.2B) on the Wikipedia page for "Abstract Factory pattern". Granted, it could use some prose alongside it. – Lightness Races in Orbit Dec 05 '12 at 22:03
  • Thank you. I also noticed that the example you linked does not use nested classes, but simply declares the needed classes in the same file of the Factory. Then why do the authors say that "this approach takes advantage of language characteristics", if in this way it is appliable to every object-oriented language? – Pippo Dec 05 '12 at 22:19
  • I'm not sure that I understand the premise of your question. Have you inferred from the authors' wording that the pattern takes advantage of, in each case, characteristics _unique to the language_? That doesn't seem likely to me. Certainly there's no need for this to be true in the C++ case, beyond the ways in which C++ already differs from the other languages mentioned. – Lightness Races in Orbit Dec 05 '12 at 22:38
  • Thank you again. Actually, this is exactly what I inferred (also from the context of the book), i.e. the fact that this kind of approach (with classes instead of objects in the frame of Prototype pattern) can be applied only with languages which have classes as first class objects. But there can be ways which really resemble this approach without implementing "class-objects", and one is exactly your example. I think this book is really cryptic sometimes. – Pippo Dec 05 '12 at 23:26

0 Answers0