Hi there.
I have a theoretical question about good coding practice.
Lets assume I have some kind of GUI component class. This component has a lot of functionality and I would like it to have many attachment points for various listeners.
Now, should I rather do this:
(please ignore the lack of camel-casing; underscores are just used for easier reading)public void add_some_listener(Listener l); public void add_someOther_listener(Listener l); public void add_yetAnother_listener(Listener l);
or would this be more user-friendly:
public void addListener(ListenerType type, Listener l); public static enum ListenerType { SOME_LISTENER, SOME_OTHER_LISTENER, YET_ANOTHER_LISTENER; }
Then, in comparison, the user could would use it like this:
public void initComponent() { /* * First approach */ add_some_listener(someListener); add_someOther_listener(someListener); add_yetAnother_listener(someListener); /* * Second approach */ addListener(ListenerType.SOME_LISTENER, someListener); addListener(ListenerType.SOME_OTHER_LISTENER, someListener); addListener(ListenerType.YET_ANOTHER_LISTENER, someListener); }
What do you think is more "clean" and easier to use?
I mean, the second approach would cut down the number of methods; the documentation would be shorter, easier to read, easier to memorize.
On the other hand the enum adds some more code to all of that; you have to simply write more stuff. There is also the problem about passing "null" as an argument.
What should I do? Is this simply a matter of taste or are there any factual benefits to either of those solutions?
Thank you very much.