Re: use case for extending enum, but this is not possible in java
Laura Schmidt <ls@mailinator.com> writes [some lines selected]:
there were no limitations in the java language, I would separate the
concerns of library and application like this:
--- library:
public enum ListCommand
public interface ListCommandProcessor
void onListCommand (ListCommand cmd);
--- application:
public enum AppListCommand extends ListCommand
OPEN,
CLOSE,
SOMETHINGSPECIAL;
public class MyApp extends ListCommandProcessor
listing.setProcessor (this);
public void onListCommand (ListCommand cmd)
AppListCommand c = (AppListCommand) cmd;
How would you do this?
library;
public class Command {}
public interface CommandProcessor
{ void onCommand ( Command cmd ) ... }
/* NB: The semantics are now actually /better/ than
in the quoted fragments, because ?cmd? is a /command/,
not a ?list command? as quoted above ! */
public class OPEN extends Command { /* possibly: ... */ }
/* NB: we have the inheritance semantics usually recommended,
because ?OPEN? /is a/ command! */
public class CLOSE extends Command { /* possibly: ... */ }
public class DELETE extends Command { /* possibly: ... */ }
application:
public class AppCommand extends Command { /* possibly: ... */ }
public class SOMETHINGSPECIAL extends AppCommand { /* possibly: ... */ }
public class MyApp extends CommandProcessor
{ ...
listing.setProcessor( this );
public void onCommand( final Command cmd )
{
AppCommand c =( AppCommand )cmd;
/* this cast will fail when( cmd instanceof OPEN ),
but this is correct, since OPEN /is not/ an AppCommand */ ... }}