Thursday, July 21, 2011

Lady Java


This video might not appeal to most, but if you look at it carefully, they have shown stuffs like Font, Graphics classes and different AWT stuffs to draw Lady Java. The concept is very good. If you pause at the part when they say stuffs about Microsoft (not my view. Microsoft is fine), they show a very funny blue screen of death.

Sunday, July 10, 2011

Adapter Pattern - Can I mix oil and water?

What is an adapter pattern?

It is a pattern from gang of four's design pattern book. It helps us ,write a wrapper, so that we can encapsulate the incompatible type as if it were always compatible.

Do we write adapter in a brand new system?

We don't do all things from scratch, so there may be times when we want to use some or all of old established code. So, we may write an adapter to use those codes too. It is also one form of code re-use. One thing now, must be clear to you is that adapter is not as fast as if it were compatible in the first place. Let us write a complete program to illustrate this pattern.

Case : I have a very old tape player, which I want to use as my cd player. Can I write a wrapper to operate it like a new cd player. Though I know there will be inconsistencies in the quality of playing and some functions might not work, but I want to do it any way.

Lets first define an interface that our modern cd player uses.


// interface

public interface CDPlayer {

    void insertCD(Album album);

    void nextSong();   

    int getPlayedTimeTillLastSong();

    int currentSongLength();   

}


Saturday, July 9, 2011

Lets learn java api programming

A piece of good code is as good as a good book

Since java is very easy to learn, expressive language. Reading good code is the best way and probably the shortest and hassle free way to adapt a good coding style. Java programmers read many java books before they consider themselves as fluent as their PHP counterpart. There are many good reasons to choose java as your primary working language and continue your profession using this language, but the theme of this post is not what java offers, but how to adapt a universal or general approach of programming, so that we can share good piece of code that everybody (including yourself) can reuse and read like a book. Lets start with an example.


// HelloWorld.java
public class HelloWorld {
   public static void main(String[] args) {
      System.out.println("Hello World");
  }
}

Make your code flexible no matter when you write it
Do you consider above code flexible and reusable? If you do, then you are missing a great feature of java's ecosystem. The bottom-line is, unless you are writing code for a (very * 500) times memory critical device or project, you want to make your code flexible enough for always, unless you find a better solution. Open source project gurus have a saying.


Friday, July 1, 2011

Should we fear unit testing ?

No. Unit testing is a super set of public static void main. And the best part is that you can have as many as you want. So how great is this, that you can test as many aspects of your program as you want. In days when unit testing was not introduced people had to write nasty logic via main method to do all those testings and even made themselves careful to not choose any objects or primitive names that conflict with each other. Well, not any more. Each part of main is executed under @Test method and they don't interfere with each other (you are a java programmer you may do nasty things to interfere). You can do what ever you did in public static void main like System.out.println and etc. And if you like unit testing a more, you may choose to display none of those output and silently say I assume this is the output, so please verify this for me. Let's see an example.