Site Sponsors:
UML Primer Updated 
Just a quick note to let everyone know that I just updated the UML Primer on YouTube. I wanted to increase the volume so as to re-use the video in a class we are teaching next week for the U.S. Army.

[ add comment ] ( 1981 views )   |  permalink  |  related link
Faster Field Support using Enumeration & Arrays 
The humble 'enum' has come a long way. --Indeed, when I first taught the 'enumeration concept' (i.e why use numbers, when you can use names) a few decades ago, I was using C/C++.

Today both .Net and Java have their humble 'enum' equivalent:

enum codes {
one, two, three
}

--Easy enough to understand.

Object Members


What is interesting to note however is that, unlike C/C++, any 'enum' in either Java or .Net is not merely about mnemonic substitution. Rather, the enum keyword in both technologies creates a fully-qualified Object:


public String get(codes which) {
return vals.get(which.ordinal());
}

public String set(codes which, String sWho) {
return vals.set(which.ordinal(), sWho);
}


Like any Object, any instance of 'enum' in .Net and Java has member functions. Amongst other things, by default the members of an 'enum' allow us to translate between a set of zero-based integers, as well as any enumerated token.

Enumerated Rarefactions


Since by default the enum uses a zero-based ordinal (read i-n-t-e-g-e-r ;), the use of any enumerated set of tokens readily transfers over to a zero-based array usage. The piratical :) application of this default numbering series is that any set of arbitrary enumerators can be used to quickly create a class, with just as many value-storage opportunities:

class ClientSettings {
ArrayList<String> vals = new ArrayList<String>();

public enum codes {
// We could also use enums like name, address, phone (etc)
one, two, three
}

public ClientSettings() {
codes[] set = codes.values();
for (codes code : set) {
vals.add(code.name());
}
}

public String get(codes which) {
return vals.get(which.ordinal());
}

public String set(codes which, String sWho) {
return vals.set(which.ordinal(), sWho);
}

public static void main(String[] args) {
ClientSettings ref = new ClientSettings();
if (ref.get(codes.two).equals(codes.two.name()) == false) {
System.out.println("Error - Conceptual failure #1.");
}
String sTest = "Enumerated arrays are so easy, they are scary.";
ref.set(codes.two, sTest);
if (ref.get(codes.two).equals(sTest) == false) {
System.out.println("Error - Conceptual failure #2.");
}
}

In short, no mater if we have 2 or 200 values. -Whenever we want to quickly create a class to support the storing of one or all of them, all we have to do is to update a single 'enum'. --The rest is as good (or evil ;) as is the enumerated set of tokens we choose to represent our data_stored therein.

Enjoy,

-Rn

(p.s: The above is a Java demonstration. The proof of concept is in main().)

[ add comment ] ( 2524 views )   |  permalink

<<First <Back | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | Next> Last>>