November 2006 Archives

Google Space ?

| | Comments (0) | TrackBacks (0)

Very funny to read Googles newest job offer. Who “on earth” wouldn´t love to work on “high-density high-delivery hosting (HiDeHiDeHo)” ?

Pattern Reminder: Singleton

| | Comments (0) | TrackBacks (0)

Definition: The pattern is used to ensure that only one instance of an class is used. It offers a global point of access to this object. Before Java5 consider double checked locking to be thread-safe.

Example: Lets think of using only one connection to whatever throughout the program.



public class Connection {
//volatile !!!!
private static volatile Connection connection;
//private Constructor
private Connection(){}
public static Connection getInstance(){
if (connection == null)//check first
synchronized(Connection.class) //one access per thread. sync only once
if (connection == null) //check again
return new Connection();
return connection;

Pattern Reminder: Command

| | Comments (0) | TrackBacks (0)

Definition: The Command pattern defines the concept of encapsulating an action as an object and lets you parameterize clients with different requests, queue requests, log requests or undo operations.

Java Example: A classic example for an implementation of the command pattern is a remote control which switches the light on and off. An example for an command queue would be a disco-lighting system.



public interface ICommand {
public void execute();
public void reset();

LightOnCommand (LightOffCommand would simply print out "light off") :

public class LightOnCommand implements ICommand {
public void execute() {
System.out.println("light on");
public void reset() {@TODO}

Remote Control:

public class RemoteControl {
ICommand SwitchOn;
public void setCommand(ICommand aCommand)
SwitchOn= aCommand;


public static void main(String [] args)
RemoteControl remote = new RemoteControl();
remote.setCommand(new LightOnCommand());
remote.setCommand(new LightOffCommand());

And this is an implementation for a command queue. Let´s go to the discotheque....


public class LightingSystem implements ICommand {
ICommand[] commands;
public LightingSystem (ICommand[] c)
{ commands = c; }
public void execute()
for (int i = 0; i < commands.length; i++) {
public void reset() {
for (int i = 0; i < commands.length; i++) {

And the test:

public static void main(String[] args) {
LightOnCommand on = new LightOnCommand ();
LightOffCommand off= new LightOffCommand ();

ICommand[] techno= {on,off,on,off,on,off,on,off};
LightingSystem strobo= new LightingSystem (techno);

Pattern Reminder: Observer

| | Comments (0) | TrackBacks (0)

Definition: The Observer Pattern defines a One-to-Many dependency between objects in a way that all dependent objects get automatically notified und updated, if the state of an object changes. Subjects or Observables notify Observers over an defined interface. Observers do not get notified in a particular order. Swing, JavaBeans and RMI use the Pattern often.

Java Example: A client registers to a newsletter and gets notified for every new news.



public class Newsletter extends Observable
private String news;
public Newsletter(){}
public void setNewNews(String text)
{ = text;
public String getNews()
return news;


public class NewsReader implements Observer {
Observable observable;
public NewsReader(Observable ob)
this.observable = ob;
public void update(Observable obs, Object obj) {
if (obs instanceof Newsletter) {
Newsletter newsletter = (Newsletter)obs;

Pattern Reminder: Strategy

| | Comments (0) | TrackBacks (0)

Starting a new series to remember the good old gof design patterns. The STRATEGY Pattern will be my first entry.

Definition: "Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it." [Gamma]

Java Example: The client uses a capsuled family of algorithms. In this case we encapsulate a behaviour.



public abstract class Fighter {
FightingBehaviour fightingBehaviour;
public Fighter(){ };
public void fight(){fightingBehaviour.fight(); }
public void setFightingBehaviour(FightingBehaviour fb) {fightingBehaviour = fb;}

Client Implementations:

public class Boxer extends Fighter
public Boxer()
fightingBehaviour = new BoxingBehaviour();


public interface FightingBehaviour {
public void fight();

Algorithm Implementation:

public class BoxingBehaviour implements FightingBehaviour {
public void fight() {
System.out.println("Raise fist!");

Test it:

Fighter fighter = new Boxer();

Thats basically it. It is now very easy to add new behaviours or fighters and change their algorithms.

About this Archive

This page is an archive of entries from November 2006 listed from newest to oldest.

October 2006 is the previous archive.

December 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.0