java - How to model an enumeration where one constant "behaves" differently? -


i have status label shows message colored border according current status. message can vary (that there can multiple messages error) clear status different, since should never display message.

example usage

setstatus(information, "there new cookies in jar."); setstatus(error, "you slow."); setstatus(error, "stop bugging me, slow."); clearstatus(); 

how can model exceptional state clear? should remove enumeration? have included enumeration, since clear valid state in except not show message. yet client call:

setstatus(clear, "ups, gave message"); 

what other options there model list of values 1 of equal nature?

public class statuslabel {     public enum status {         clear,         information,         error;     }      status status = status.clear;      public void setstatus(final status status, final string message) {         assert status != status.clear;         // set status , show message         this.status = status;     }      public void clearstatus() {         // clear message         status = status.clear;     }      public status getstatus() {         return status;     } } 

even though think special value none or null absoluly ok, no problem model different behavior of enum values. have consider, each of these values can have it's own class , such it's own unique behavior.

    public class statuslabel {         public enum status {             none {                 public void show(string msg) {                     throw new illegalargumentexception("don't show clear!");                 }             },             information,             error;             public void show(string msg) {                 // ever needed             }         }          status status = status.none;          public void setstatus(final status status, final string message) {             status.show(message);             this.status = status;         }          public void clearstatus() {             // clear message             status = status.none;         }          public status getstatus() {             return status;         }     } 

this way enum value really behave different.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -