Switch it up
Anyone who has done application development for very long has likely run across the problem of having to work with settings (ie what options are turned on, what options are turned off) and how to store them.
Let's use the following case:
Your application currently has four modules, but may grow to include more. Each module can be independently turned off or on on a user by user basis. User profiles are stored in a database and when a user logs into the system the application creates an instance of a 'user' class to store and access information about the user. Two problems arise. First, if the application module count will increase how can additional switches be added to the user profile without having to extend the user table. Second, how can you easily work with the switch values returned from the database without having to add extra code to the user class.
Enter the binary string .... 11110110101, behold the beauty of 0's and 1's. A string of 0's and 1's, or true's and false's if you may, little switches lined up and just waiting to be used. So how can you store and access this powerful little creature. A number can easily be converted between bases (ie base 2, base 10, base 16). Base 2 is binary (01010101). Base 10 is decimal, how you normally see numbers (0123456789). Base 16 is hexadecimal (ff9900) this will look familiar to anyone who has used html colour values.
The ability to easily change the base of a number means we can retrieve a single number, convert it to base 2 (binary) to get the switch values we need, adjust any of the switch values, and then convert the binary string back to base 10 and store it again as a single numeric value.
Here are some examples of values in decimal and the corresponding binary value (note binary strings always read from right to left, the rightmost value is the least significant digit, decimal 0 or 1.
- 1 = 1
- 2 = 10
- 3 = 11
- 4 = 100
- 100 = 1100100
- 1000 = 1111101000
We now have a way to store one value in the database and the ability to turn that value into a set of switches to determine if something is on or off. In the example mentioned above we could represent any combination of the four modules being on or off with an integer value between 0 and 15.
The sample above uses my 'BinarySwitch' class to manage a set switches. By defining a set of switches the class can set/get the value for any switch or set/get the total switch value. Download the source here.
In our sample case adding an extra module would not require any extensions to the database or the user class. In the database could have one column called 'module_settings' in the 'users' table that would store the numeric value of the switches. The user class could contain a property called 'module_settings' that would store the numeric value returned from the database when the user profile is retrieved. In our application code we could use the following snippet to either set or get the user's module settings.
import com.dgrigg.data.BinarySwitch;
var moduleSwitch = new BinarySwitch();
var mods = ['module1', 'module2', 'module3', 'module4'];
moduleSwitch.setSwitches(mods);
moduleSwitch.setValue(userProfile.module_settings);
//check if the user has access to module 2
if (moduleSwitch.getSwitch('module2') == 1){
//user has access
} else {
//alert the user that they do not have access to this module
}
If a new module gets added to the application all we need to do is add the module name in the array that sets up the switches
var mods = ['module1', 'module2', 'module3', 'module4', 'module5'];
moduleSwitch.setSwitches(mods);
To set a switch value in the user's profile and get the total switch value for storage in the database is equally easy.
import com.dgrigg.data.BinarySwitch;
var moduleSwitch = new BinarySwitch();
var mods = ['module1', 'module2', 'module3', 'module4'];
moduleSwitch.setSwitches(mods)
moduleSwitch.setValue(userProfile.module_settings);
//update the users access to module 3
//based on the value of module3 checkbox value
moduleSwitch.setValue('module3', module3_chbx.selected ? 1 : 0);
userProfile.module_settings = moduleSwitch.getValue();
Who knew 0's and 1's could be so useful.
Jan 04, 2008 at 7:25 AM
Can you please help me with this? Please check and tell me what you think about that: http://www.expressdelivery.biz Thanks in advance