Skip to content
Advertisement

How to implement a factory class?

I want to be able to create objects based on an enumeration class, and use a dictionary. Something like this:

JavaScript

But I get the error:

JavaScript

For some reason the dictionary can’t be created. Where am I going wrong here?

Advertisement

Answer

After looking at Bruce Eckel’s book I came up with this:

JavaScript

This gets the user to select a class type from the enumeration, and blocks any other type. It also means user’s are less likely to write ‘bad strings’ with spelling mistakes. They just use the enums. The output from the test is then, something like this:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement