code

Friday, November 19, 2004

Introduction To Pivot Tables

Man I've always hated getting bested by some excel wonk about Pivot Tables....ooooh. Now i have this. Watch out wannabes.

Introduction To Pivot Tables

Thursday, November 04, 2004

Singletons with needles and thread

Some more....
Singletons with needles and thread: "The singleton factory
The singleton factory creates the singleton instance and guarantees that there will be only one instance in the system at any given time.

Here's an implementation of a factory that will return the LogImpl:


public abstract class AbstractLogFactory {

private static AbstractLogFactory factory = new SystemLogFactory();

public static AbstractLogFactory getFactory() {
return factory;
}

public abstract LogSingleton log();

}

class SystemLogFactory extends AbstractLogFactory {

private static LogSingleton log = new LogImpl();

public LogSingleton log() {
return log;
}
}

If you program your objects to retrieve a factory only from the AbstractLogFactory, you can switch logs without changing the classes that use the factory. When a new log you wish to use comes along, simply create a subclass of AbstractLogFactory that instantiates the proper type of log and update AbstractLogFactory to return the proper factory type.

Thus, if you would rather return a FileLog, you could make the following changes:


public abstract class AbstractLogFactory {

private static AbstractLogFactory factory = new FileLogFactory();

public static AbstractLogFactory getFactory() {
return factory;
}

public abstract LogSingleton log();

}

class FileLogFactory extends AbstractLogFactory {

private static LogSingleton log = new FactoryLog ();

public LogSingleton log() {
return log;
}

}

This alternate approach to singleton creation proves more flexible than other approaches because you move the Singleton-pattern enforcement responsibility from the implementation and to another object. In fact, this approach's flexibility lets you change from returning a singleton to creating and returning multiple instances -- all without the rest of your program knowing."

Singleton

Small review on singletons.

Singletons with needles and thread:



public class Singleton {

private static Singleton instance;

public static Singleton getInstance() {
if( instance == null ) {
instance = new Singleton();
}
return instance;
}

private Singleton() {}

public static void main( String [] args ) {
Singleton instance = Singleton.getInstance();
// ... manipulate instance
}

}

Unfortunately, in the example above, a thread may at any time pre-empt the call to getInstance(). For example, a thread may pre-empt a running thread at instance = new Singleton. If that were to happen, multiple Singleton instances might instantiate, thus defeating the purpose of a singleton.

To make a singleton thread safe, you have two choices. The first simply synchronizes the getInstance() method:

public class Singleton {

private static Singleton instance;

public synchronized static Singleton getInstance() {
if( instance == null ) {
instance = new Singleton();
}
return instance;
}

private Singleton() {}

public static void main( String [] args ) {
Singleton instance = Singleton.getInstance();
// ...
}
}

Synchronizing the method guarantees that a call to the method cannot be interrupted.

The second approach to thread safety declares a constant Singleton attribute on the Singleton class itself:

public class Singleton {

public final static Singleton instance = new Singleton();

private Singleton() {}

public static void main( String [] args ) {
Singleton instance = Singleton.instance;
// ...
}

}


instance will initialize when the class loades. Both solutions guarantee only one Singleton will exist.

"

Monday, November 01, 2004

TWiki . People . SmellsToRefactorings

My newest happy place is Refactoring to Patterns. There are some good books on it, well, one. Here's a web resource.

Patterns are good, but since most of us inherit designs rather than creating new ones, Refactoring to patterns has helped me grok the patterns value more quickly.

TWiki . People . SmellsToRefactorings