This chapter is from the book

This chapter is from the book

Leveraging the Name

Another way that we can simplify the implementation of abstract factories is to rely on a consistent naming convention for the product classes. This approach won’t work for our habitat example, which is populated with things like tigers and frogs that have unique names, but imagine you need to produce an abstract factory for objects that know how to read and write a variety of file formats, such as PDF, HTML, and PostScript files. Certainly we could implement IOFactory using any of the techniques that we have discussed so far. But if the reader and writer class names follow some regular pattern, something like HTMLReader and HTMLWriter for HTML and PDFReader and PDFWriter for PDF, we can simply derive the class name from the name of the format. That’s exactly what the following code does:

The const_get method used in IOFactory takes a string (or a symbol) containing the name of a constant1 and returns the value of that constant. For example, if you pass const_get the string "PDFWriter", you will get back the class object of that name, which is exactly what we want in this case.2