Whether it is art, a movie, a book, a design, a business idea - you can find flaws in it, you can redicule it, and you can dismiss it. For it is a far greater skill to create than it is to destroy. In reality not one of us are masters of an entire body of knowledge, the greatest strength of humans is communication and to work together in a common goal.

The next time you find your work being attacked, use it as an opportunity to add to your idea. Ask them for a better idea, to add to your idea. One of two things will happen: the attacker will have nothing to offer and you can dismiss them or the attacker may offer information good or bad. Either way you will either learn something about the attacker, your creative process, or both.

And the next time you find yourself attacking the end result of someone's creative process; either take a step back and appreciate the differences between each of our thought processes as individuals or discuss the creative process itself, adding to their work and not destroying it.


"Thus, what is of supreme importance in war is to attack the enemy's strategy"
--- Sun Tzu

Writing about design patterns made me think about patterns in my own life. Have you ever thought about the repeated things you do? Some of them may be good, you work out everyday after work. Some of them may be bad you wake up late and then run around trying to get things together before you go to work or you may watch TV for four hours straight wasting the day. I believe that by examining our patterns and our behavior we can find patterns that are good, and patterns that are bad.

Anti-patterns in software development are patterns that seem obvious yet are ineffective or sub-optimal. Some people argue that a singleton is an anti-pattern because it doesn't allow the client to determine how many instances are needed, they carry state with them through the life of the program, they destroy the concept of single responsibility, and they effectively become a global variable.

Either way learning the singleton is by far the easiest way to start learning design patterns and is something every developer should know. Thinking about context of classes is crucial to deciding if this pattern is the right for your application.

Continuing on with the earlier discussion of simple interview questions every developer should know I describe here the singleton design pattern. Along with being able to answer the questions regarding a singleton, a developer should be able to write a class that implements the pattern.

What is a singleton?

A singleton is a design pattern that allows for only one instance of a class to be created.

Why would you use a singleton?

Singletons permit lazy initialization as well as avoiding global variables.

A simple example: Suppose that you have to load a table of state names and abbreviations into memory from a datastore that will be used by your application. You could write a static initializer in your class to retrieve the states, however it would be loaded even if you do not require the data. You could also create a class that retrieves the data from the the datastore every time someone needs it, however you realize that the data is static and will be requested many times which may impact performance.

Assuming that you are creating a web application where the states will populate a select box. The first thing you will write is a simple bean to encapsulate the data:

public class StateBean {

  private String abbreviation;
  private String name;

  public StateBean() {
    super();
  }

  public String getAbbreviation() {
    return abbreviation;
  }

  public void setAbbreviation(String abbreviation) {
    this.abbreviation = abbreviation;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

}


Next you will need to write the singleton to retrieve the data and populate a collection of StateBeans. Since you will walk through the collection using tags in a JSP page you will need to return this collection. There is a private static INSTANCE object of the same type as your singleton class. There is a private constructor, which stops it from being created outside of its own class, and there is a getInstance() method that returns the only instance. The other methods are self explanatory and will be used to get the list of states and populate the list of states.

import java.util.ArrayList;

public class StateSingleton {

  private static StateSingleton INSTANCE = null;

  private StateSingleton() {
    populateStatesList();
  }

  public static StateSingleton getInstance() {
    if (INSTANCE == null)
      INSTANCE = new StateSingleton();
    return INSTANCE;
  }

  private ArrayList statesList;

  public ArrayList getStatesList() {
    return statesList;
  }

  private ArrayList populateStatesList() {
    statesList = new ArrayList();
    // Code to retrieve states
    // Normally this would be done from a datastore
    // But we will just add some manually
    statesList.add(new StateBean("OH", "Ohio"));
    statesList.add(new StateBean("NY", "New York"));
    return statesList;
  }
}


Finally let's create a client to call the singleton:

public class StatesClient {

  public static void main(String[] args) {
    StateSingleton states = StateSingleton.getInstance();
    for (int i = 0; i < states.getStatesList().size(); i++) {
      StateBean state = (StateBean) states.getStatesList().get(i);
      System.out.println("Abbreviation: " + state.getAbbreviation()
          + ", State: " + state.getName());
    }
  }

}

JavaScript can be used to determine what version of PDF your browser supports using various methods. I believe that JavaScript can only determine the major version of an installed Acrobat Reader plugin, and not the minor version.

For Internet Explorer you can check if the ActiveXObject exists for different versions of Acrobat Reader. The ActiveXObject name for version 7.0 and 8.0 is "AcroPDF.PDF.1", for other versions it is found by the ActiveXObject name "PDF.PDFCtrl." concatenated with a version number. The only exception is version 4.0, and you can review the code below for an explanation.

For FireFox and other browsers you can find the plugin named "Adobe Acrobat" which will contain a description of the plugin and its version. The description for version 8.0 is "Adobe PDF Plug-In For Firefox and Netscape" while other versions contain the version number.

The following three functions will help in determining the version of PDF the client browser supports. I have left out cross-browser support as that is a problem that has already been solved in multiple ways.

/**
* Determines version of the Acrobat Reader plugin for FireFox and Netscape
* @returns the major version of the plugin and returns null if PDF is not supported
**/
function OtherPDFVersion() {
  var version = null;
  var plugin = navigator.plugins["Adobe Acrobat"];
  if (plugin == null) return null;
  if (plugin.description == "Adobe PDF Plug-In For Firefox and Netscape") {
    version = '8.0';
  } else {
    version = plugin.description.split('Version ')[1] + '.0';
  }
  return version;
}

/**
* Determines version of the Acrobat Reader plugin for InternetExplorer
* @returns the major version of the plugin and returns null if PDF is not supported
**/
function IEPDFVersion() {
  var version = null;
  if (hasActiveXObject('AcroPDF.PDF.1')) {
    version = '7.0';
  }
  if (hasActiveXObject('PDF.PdfCtrl.1')) {
    version = '4.0';
  }
  for (var i=2; i<10; i++) {
    if (hasActiveXObject('PDF.PdfCtrl.' + i)) {
      version = i + '.0';
    }
  }
  return version;
}

/**
* Helper method to determine if IE contains an ActiveXObject with a given name
* @param name - the name of an ActiveXObject
* @returns true if the browser contains the ActiveXObject
**/
function hasActiveXObject(name) {
  var has = false;
  try {
    activeXObject = new ActiveXObject(name);
    if (activeXObject != null) {
      has = true;
    }
  } catch (e) {
    has = false;
  }
  return has;
}

Here is an interesting use of reflection in Java. The following class is HelloWorld with no semicolons. The interesting thing about Java is that methods can return void which cannot be used in an if statement. By using reflection you can invoke a method that returns void, and have it return an Object.

public class HelloWorldNoSemicolons {

  public static void main(String[] args) {
    try {
      if (null == System.out.getClass().getMethod("println",
          new Class[] { Class.forName("java.lang.String") }).invoke(
          System.out, new String[] { "Hello World!" })) {
      }
    } catch (Exception e) {
    }
  }
}

"Write the bad things that are done to you in sand, but write the good things that happen to you on a piece of marble." --- Arabic Proverb

30 is the new 40.

I travel for work, therefore I fall asleep and wake up at irregular times. I tracked my time after a particularly busy 10 days and found that I had -3 hours of personal time. There must be a way to have "mini-vacations" during the course of a normal month. It is amazing how after laying around on a beach for a few days you feel like a new person. I am starting to believe that clearing your calendar to just "be" is an extremely important piece of the puzzle of life.

I keep re-writing the following script for cygwin/bash because I find myself working on proof of concept code, and new environments without source control systems in place.

I am posting it for two reasons: I will need it again and it is easier to remember cygwin/bash from my own examples.

The following script versions all new files in a directory:


#!/usr/bin/bash

# The directory we are backing up to
backupdir="/cygdrive/c/backup";

# Get the directory that will be backed up
dir=$1;

# Give user information
echo "Versioning files in: $dir";
echo "Current backup in: $backupdir";

# If datestamp.file is not found then
# this is a new directory, no changes found
if [ ! -f $dir/datestamp.file ]
then
  echo "Initial backup request";
  echo '.' > $dir/datestamp.file;
else
  datestring=`date +%D-%H-%M | sed 's/\//-/g'`
  #echo "datestring: $datestring";
  for f in `find $dir -name "*" -newer $dir/datestamp.file`;
  do
    if [ $f = "." ]; then
       echo "Ignoring: $f";
    else
       #echo "Currently processing: $f";
       f2=${f/\./\/$backupdir};
       if [ -d $f ]
       then
         echo "Creating directory: $backupdir/$f2";
         mkdir -p $backupdir/$f2;
       else
         f2=$f-${datestring};
         f2=${f2/\\./\/$backupdir};
         echo "Versioning file to: $backupdir/$f2";
         cp $f $backupdir/$f2;
       fi
    fi
  done
fi

Remember when you learned how to ride a bike? You would get on the bike, unsure of what to do, afraid you would fall. But you would try anyways. And you didn't know what to do, and you did fall. In fact you got hurt. But you got back on, and learned how.

What if success in life is due partially to the fact that you tried? I believe that this is true. If you list some of the things that you would like to accomplish, I bet that you would find that there are two types: One where you know what to do, and one where you don't.

For the ones where you know what to do, all that is required is action. For the ones that you don't know what to do, all that is required is learning plus action.

Action doesn't equal success, just the opportunity for success. Inaction however does equal failure.

I have had the opportunity to interview several Java developers who claim to have 5+ years of development experience, yet they fail to answer this simple question. This tells me one thing: You are not learning your profession.

What is an abstract class?

An abstract class is a class marked abstract, it may include abstract methods. Abstract classes can not be instantiated, but can be subclassed. Abstract classes provide a partial implementation, leaving it to subclasses to provide the complete implementation.

Evacipate v. To undo the consequences of your actions by changing the original circumstances. To erase your own history. Evacipated v. p. pl.