Factory classes are types that can fabricate objects for us. They can help decouple polymorphic object types from their callers. They can allow for using object pools (in which reusable objects are kept so that you don't need to constantly allocate and free them) or other allocation schemes. Those are just a few examples of how they can be useful. Let's take a closer look at yet another one. Imagine you need to create different polymorphic types based on input parameters. In some cases, a polymorphic factory function such as the one shown next is not enough:
std::unique_ptr<IDocument> open(std::string_view path) { if (path.ends_with(".pdf")) return std::make_unique<PdfDocument>(); if (name == ".html") return std::make_unique<HtmlDocument>(); return nullptr; }
What if we wanted to open other kinds of documents as well, such as OpenDocument text files? It may be ironic to discover that the preceding open...