lukebevan.com Blog A Computer Science Student from the University of Wolverhampton, UK

16May/110

Finished University

Hi everyone, sorry I haven't uploaded this blog in months, it's because I've been busy with university work. I have now finished university and I am now looking for a graduate job, which gives me plenty of time to update this blog. During my final year at university, I've done modules such as Advanced Software Engineering Topics (Design Patterns and Constraint Programming), Advanced Web (Object Orientated PHP, AJAX and Web Services) and my final year project (Dissertation).

Over the next few days I am hoping to write a few book reviews of the books I have used during my final year, such as Head First Design Patterns and Harnessing Hibernate. At the moment, I'm reading a book called Learning Android which is published by O'Reilly Media and I'm hoping to write a book review after I finished reading it. In the meantime I've been looking for a graduate job. A job for a year or two would be useful for towards a Masters degree and the fact I can get a equal balance of academic and job experience. After reading about Android development, I'm hoping to read more about C# and ASP.NET, mainly because of the availability of jobs available for these languages.

Android Logo in 3D

Android Logo in 3D

22Jul/100

Design Patterns with Java (Adapter Pattern)

The Adapter Pattern is used to convert the interface of a class into another interface the client expects. This design pattern also allows classes to work together even if they have an incompatible interface, which means this pattern can be used to integrate another system with an existing system. Below is the code sample to demonstrate this pattern.


public interface Car {

  public void drive();

  public void navigate();

}


public interface Airplane {

  public void fly();

  public void navigate();

}


public class SmallCar implements Car  {

  public void drive(){

    System.out.println("Small car has moved");

  }

 public void navigate(){

    System.out.println("Small car is now moving in the right direction");

  }

}


public class CarToAirplaneAdapter implements Airplane{

  Car car;

  public CarToAirplaneAdapter(Car car){

    this.car = car;

  }

  public void fly(){

    car.drive();

  }

  public void navigate(){

    car.navigate();

 }

}

CarToAirplaneAdapter implements the methods of the Airplane interface but calls methods on a car object. This adapter also accepts an car object reference in its constructor. The car object is wrapped in an Airplane adapter enabling it to behave like a airplane, this means the code has now been adapted.

Thanks to Head First Design Patterns.



21Jul/100

1 in 13 people in the world has a Facebook account

Facebook logo

Social networking website Facebook has registered its 500 millionth account, which means when calculated  1 in 13 people on Earth (6.8 Billion est.)  has an account on Facebook, which is an amazing achievement for any website. The United Kingdom has 26 million users alone (42% of the British population). Social networking has increased dramatically from the middle of last decade to now, thanks to websites such as Facebook, MySpace and YouTube. YouTube is not technically a social networking website but the site does have a social aspect, such as the ability to add friends and comment channels and videos.

In recent years MySpace has fallen in decline as Facebook became more prominent, mainly because Facebook appeals to a larger age range when compared to MySpace and Bebo, which targets the teenage market. Micro-blogging has been a major contributor to online activity in recent years, with websites such as Twitter arriving on the scene, which has reached an impressive 15 billion tweets this May.
.

Sources:  http://www.bbc.co.uk/news/technology-10713

http://www.guardian.co.uk/technology/2010/jul/21/facebook-500-million-users

21Jul/100

Design Patterns with Java (Encapsulating Invocation)

Over the last few months I have been preparing for my final year for my BSc Computer Science (Software Engineering) degree, during this time I have read Head First Object Orientated Programming with Java,  Java Pocket Guide and Head First Java. During the past year I have been on an Industrial Placement as an I.T. Technician at King Edward VI College in Stourbridge, I will resume my studies on the 13th September.

Below is some  Java code of a design pattern called the "Command Design Pattern", this design pattern enables to change the command's behaviour during runtime as the code below is "loosely coupled". The reason why it is loosely coupled is because the command the button uses is not hard-coded and as explained, the command can be changed at runtime.  The code block below demostrates that a Command object can be used by a button object and the Command object can be simply changed by invoking the  setCommand() method on the button object.


public class CommandTest {

	public static void main(String[] args) {

		Command openDoorCommand = new OpenDoorCommand(new Door());
		Button button = new Button(openDoorCommand);

		button.doCommand();
		button.undo();
		button.setCommand(new OpenWindowCommand(new Window()));
		button.doCommand();
		button.undo();

	}

}

The code below is for the button, the button uses a command object. Here the command's execute() method can be invoked with in the doCommand method (which was called in the main method of the CommandTest class), the button can use any object unless it implements the Command Interface. The command can be changed by using the setCommand method if required.


public class Button {

	Command command;

	public Button(Command command){
		this.command = command;
	}

	public void doCommand(){
		command.execute();
	}

	public void undo(){
		command.undo();
	}

	public void setCommand(Command newCommand){
		command = newCommand;

	}

}

This code contains what the command does, the execute() method can open a "door" object and the undo() method can close the door, this class implements the Command interface. The code below also enables the encapsulation of invocation as a button's command can be changed during runtime and a command object can interact with another object, in this instance a door.


public class OpenDoorCommand implements Command {

	Door door;

	public OpenDoorCommand(Door door){
		this.door = door;
	}

	public void execute() {
		door.open();

	}

	public void undo() {
		door.close();

	}

}

The code below is the same as OpenDoorCommand, just replace "Door" with "Window", remember OpenWindowCommand must use a Window object in its parameterised constructor.


public class OpenWindowCommand implements Command {

	Window window;

	public OpenWindowCommand(Window window){
		this.window = window;
	}

	public void execute() {
		window.open();

	}

	public void undo() {
		window.close();

	}

}

The code below is the Door class, an instance of a Door object can open and close, these methods are called by the OpenDoorCommand.


public class Door {

	public void open(){
		System.out.println("The door has opened");
	}

	public void close(){
		System.out.println("The door has closed");
	}

}

The code below is the same as the Door class, I have only replaced the word "Door" with "Window" as a window has  similar function when compared to a door in the real world.


public class Window {

	public void open(){
		System.out.println("The window has opened");
	}

	public void close(){
		System.out.println("The window has closed");
	}

}

This is the code for the Command interface, all commands implement the Command interface as well as the execute() and undo() methods, this is to ensure all commands have these methods so they can be called by the button's doCommand() and undo() methods. This also enables the button to use any command object.


public interface Command {

 public void execute();
 public void undo();

}

Credit to Head First Design Patterns by Eric Freeman, Elizabeth Freeman with Kathy Sierra and Bert Bates published by O'Reilly. I am only a third through the book and it did take me a while to get my head round the command pattern, but after a while it actually makes sense.