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.

3 comments:

FlyingHorse said...

Do you mean Instance Messenger instead of Instance Massager?

Anonymous said...

You write very well.

Unknown said...

Sorry for the typo. Correct.