Saturday, September 01, 2007

When to use inner class


This is a question from a friend of mind.



In a class, we use private fields and private methods to hide, or encapsulate logic from outside. However, sometimes, we need a full functioning class within another class, but the class inside should not in anyway accessible or even exist outside the bigger class. This is the situation we need a inner class.



For example, inside a watch, there are many gears. As a user of a watch, you never care how the gears work within the watch. You even don't care if a watch is work on gears or not (it maybe a electronic watch). In this case, gear could be a inner class in a watch.



Private inner class provides better shielding of complicated logic within a class. The most common case of using an inner class is implementing Listeners inside another class. You just use an anonymous inner class that implements a specific Listener interface.



Although you can also define an inner class as a public class, but usually I do not recommend programmers to do this. Because you can always extract public inner class as an ordinary public class. I think it make no sense to use a public inner class unless you want to do something dirty.

Saturday, August 25, 2007

Rethink Java and Object Oriented Software Engineering


I worked as a software developer for over 10 years. My job gives me many opportunity to lead and mentoring developers with very few or no experience on Object Oriented Software Design and Java programming. They usually ask me many questions. Some of these questions are very conceptional and not every experienced Java programmers can answer them well. If you want to find an answer in books, most books are too practical so they can just tell you how-to but seldom tell you why.


Most of them send me those questions with Instance Messenger . It is very difficult answer them with IM since it usually requires hundred of words to explain my idea. It is also a nightmare to send source code with IM. That's why I decided to answer those Java and OO Design FAQ with my blog.


As the very first chapter of the series. I'm going to recap the brief history of Java, and, as one of the most accepted language for OO Software Development, some characteristic of the language that make it so popular. If you have been using Java for a long time. You may not aware of these features have been helping you a lot. If you are programmer that have only use other computer languages like VB or PL/SQL. Please read on and you'll know why so many programmers shifted to use Java for software development.


Before the debut of Java, OO software engineering had been a hot topic. Like many new technologies, at first it was discussed in the academic circle. Then many research work was done in universities. There are quite a number of OO computer languages was born in 60s and 70s of last century. One of them was Modula 3, it have been widely used in OO software design classes in universities, but now it is widely forgetten except in university campus. Later on, Stroustrup, a Bell Labs worker, added OO feature to the C language, which was one of the most popular computer language in that age. C++ appeared in 1985, rode on the similarity with the C language, C++ gained much popularity in a decade. Finally, Microsoft also adapted C++ as a language for developing applications on Windows platform, which is the well known Visual C++.


As C++ jumped from the academic to the business world. People found that the OO feature is too difficult for junior programmers to learn. Althought it is very extensible like the C language. There is no API standardization under C++. Making C++ code not portable between different platform. There are also some disadvantage that are inherited from the C language.

If you have not used C++ before, it is hard to imagine what the problem is. C++ is like Java with following difference:

  • Supports class multiple inheritance, you can have a class extends from many classes.
  • Don't have Interface
  • Can access memory directly with pointers, some how you must use a pointer in many cases when you use an object.
  • No auto garbage collection (if you don't know what is GC, you are spoiled by Java)
  • Cannot "Write once, run anywhere", not even "Write once, compile anywhere"
If you know Java, you should have a rough idea now.


In early 1990s, James Gosling (known as the father of Java, YES!!) from Sun Microsystem, invented the Java language. It looks like C++ a lot but there are also many improvement like:
  • Simplified object inheritance
  • No custom operator
  • No pointers, so object is object everywhere in you program, like Visual Basic
  • Auto garbage collection, no more memory leak issue
  • Have its own standard API library, for many common operation like I/O, String manipulation, date manipulation, networking I/O and database access.
  • Last but not least, the mighty "Write once, run anywhere".
As a computer language, Java is much easier to learn and use than C++. Over a decade, Java continuously evolved to catch up the growth of the industry and the demand of Java programmers. That's what brings Java to the current place in the industry.


So much for the history, now we have a good OO language. However, before Java get the big piece of pie in software development industry. There were many other tools exist like PL/SQL, Power Builder, and Visual Basic. All of them have been very popular. They are not OO. Why OO is so important? Why OO design make software development easier. Many books about OO give you the answer, but most of them are very difficult to understand. Now I try to make it easier with an example.


Imagine you go to a restaurant, you sit down and a waiter comes to you. You give him your order. He takes your order, writes it on a piece of paper. Then he goes to the cashier, leaves the paper there for recording. Then the waiter himself walks into the kitchen and starts to cook. After a while your dishes are ready and the waiter brings them to your table from the kitchen.


You finish your meal, you ask a waiter to give you your bill. The waiter goes to the cashier, searches for the paper that recorded all your orders. Then he calculates the amount your need to pay. Then he comes back to your table with your bill.


Consider this restaurant is a software system. All "waiters" (actually they are also cook and cashier) are programmed by procedure based programming language like C language or PL/SQL. In this kind of system every program must works from end to end. Every program must handle every task that lies on its path. It is very difficult to make changes to this kind of system. If you want to change this restaurant from serving French dishes to Chinese ones. You need to fire all "waiters" and re-employ a new team.


Usually, we won't see a restaurant operate in this way because our world is a world of objects. Waiters, cooks and cashiers are different party but work together. There is separation of duties, which means a waiter don't need to know how to cook, and a cook don't need to know how to calculate the bill. In OO design we call this "separation of concern", which is the most fundamental idea of OO Software Engineering. A software system should be build with a group of objects that are inter-related. This is what we call a "object model". All features that a OO computer language provides can help the developers to realize the object model easier.


If you understand this idea, the new question waiting for you is: "How to build a object model correctly?" This is not a easy question, thanks to many gurus who do a lot of work of OO design. Now we have some loose guide lines called "Design Pattern". Those are answers to many common problems that we'll face in OO design. I'll talk more about them in my later articles.

Friday, March 23, 2007

Why Free Software Sometimes Hurts

I've been assigned to evaluate a free software, which is a PHP package. I downloaded the package and read through a very brief "readme.first" document. Which indicated that it requires Apache, PHP, and mySQL. Though I never installed PHP before. I thought such combo is a very common and popular combination and I didn't expect too much twist and turn.

First I installed Apache 2.2 for Windows. I had done that thousands times in my life. Then I downloaded and installed PHP5 for Windows. The installer provides automated config for Apache. Pretty!! Finally, I installed mySQL and got it up and running right away.

Everything was ready. I copied the PHP package under the web root and tried to start it. Dada!! Failed. An error message was found in the error log which looks like a program bug in the PHP package. I went through the manual of PHP and asked someone who know PHP better than me. Finally after one day of struggling, I got it working. In fact it could be much simpler. Let see why it took me so long.

1. The PHP5 installer do insert some lines to httpd.conf to load the PHP Apache module, but it forgot an "AddType" statement. I have to add it myself after I found that it is necessary, consulting the installation document.

2. Most PHP programmers use "<? ... ?>" to surround PHP code in the PHP files. While some other PHP tutorial suggest it can also be done like "<?php ... ?>". All files in the PHP package I was evaluating use the pattern "<? ... ?>". However, the default setting of PHP enforce us to use "<?php ... ?>". There is a tiny switch in the config of PHP to enable the use of "<? ... ?>". I need to turn on the switch to make the package work. Hey, why the switch is not turned on by default? Seems to me all PHP programmers prefer the shorter form.

3. Third issue is the connection with mySQL. After more reading of PHP manual, I found that the mySQL connection component is no longer included in PHP5 by default. It turns out that I needed to do 3 things for mySQL connection. First one is I needed to change the "PATH" environment string to let the system found the file "libmysql.dll", which is provided by mySQL. Second is I needed to re-run PHP5 installer to add the mySQL extension for PHP. Finally I modified PHP config for loading the mySQL extension into memory. Why can't they better document these procedures?

Sometimes free software may cost your valuable time in a unexpected way. For some other software which you need to pay, you can call technical support and get your problem solved right away (well, sometimes it may take longer). Luckily I'm never a great fan of PHP.

Friday, February 16, 2007

Under the hood of J2EE Clustering

A very nice article about Clustering in J2EE server. Worth a read.



Click here to read.