"Collection" Avoid switch! Use enum!

Source: Internet
Author: User

Today I saw an article about how to avoid using the switch and use an enum, after looking at the feeling that the author is very good, here to share.

Original address: Avoid switch! Use enum!

Recently I is about to refactor the some code crap4j pointed me to. When I realized most of this code was some kind of switch-case or If-else-cascade, I remembered Daniel´s Post and decided To obey those four rules.
This the post is supposed to give some inspiration in how to get rid of the code like:

Switch (value) {case some_constant://do Something, Case some_other_constant://do something else break; defaul T://do something totally different break; }

or an equivalent if-else-cascade.

In a, let's assume the constants used above are some the enum you kind. For example:

public enum Status {ACTIVE, INACTIVE, UNKNOWN}

The switch-case would then most probably look like:

Switch (getState ()) {case INACTIVE://do something break; case ACTIVE://do something else break; case UNKNOWN://do some thing totally different break; }

In this case you don ' t need the switch-case on all. Instead, can tell the enum-do all the work:

public enum Status {INACTIVE {public void dosomething () {//do Something}}, ACTIVE {public void dosomething () {//do Something Else}}, UNKNOWN {public void dosomething () {//do something totally}}; public abstract void dosomething (); }

The switch-case then shrinks to:

GetState (). dosomething ();

But What if the constants are defined by some code? Let's adapt the example above to match this scenario:

public static final int INACTIVE = 0; public static final int ACTIVE = 1; public static final int UNKNOWN = 2;

Which would result in a switch-case very similar to the one above and again, don ' t need it. All need to does is:

Status.values () [GetState ()].dosomething ();

Regarding this case there are a small stumbling block, which your have to pay attention to. Enum.values () returns an Array containing the elements in the order they are He one of the constants. Furthermore ensure that you don't run into a arrayoutofboundsexception. Hint:time to add a test.

There is yet another case this may occur. Let's pretend you encounter some constants that aren ' t as nicely ordered as the ones:

public static final int INACTIVE = 4; public static final int ACTIVE = 7; public static final int UNKNOWN = 12;

To cover this case, your need to alter the enum to look something like:

public enum Status {INACTIVE (4), ACTIVE (7), UNKNOWN (a), private int state; public static Status getstatusfor (int desired {for (Status status:values ()) {if (desired = = Status.state) {return Status}}//perform error handling here//e.g . Throw an exception or return UNKNOWN}}

Even though this introduces a if (uh oh, didn ' t obey Rule #4), it still looks much more appealing to me than a Switch-cas E or If-else-cascade would. Hint:time to add another test.

Feel about this technique? Got good or bad experiences using it?


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.