Archive for February 2009
The Observer Pattern
As programs grow and get more complex. the Communication between classes problems grow bigger and bigger.
to deomenstrate the two-way communcation Problem, consider the followuing example:
A Window object contains one or more UI objects inside it, say a progress bar. So, the Window object contains a progress bar object inside it as a memeber variable. The window object has a complete control of the Progress bar object – because it owns it !!. So, when the window object wants to Communicate with the progress bar, it will simply call one of its methods. BUT WHAT to do if the progress bar object wants to Communicate with its parent Window??
The Observer Design Pattern is the pattern to solve such problems – among other similar problems. It allows 2-way communication between multiple Classes. The pattern consists of the following participant classes:
- Subject: This is an interface to provide the methods that allow the progress Bar object to communicate with its Container Object – the Window-. The Subject interface provides the following APIs:
- Attach() : to add an observer to track my changes.
- Notify() : this method does the actual Communication between the Class and the Observer(s).
- Concrete Subject: the Actual class that needs to inform its owner with the change in its state, in our case, it will be the Progress Bar object.
- Observer: The interface that provides the ability to listen and react to the sent messages from the Concrete Subjects. it has a very important abstract method called update() which is Called by the Subject when sendin a message.
- Concrete Observer: this Class is the One Listening for the messages sent from the Concrete Subject object. It is the Consumer of its messages.
The following Class Diagram shows the classes of the observer pattern:

the Class Diagram of the Observer pattern
So, the following scinario occurs when applying the pattern :
- The Concrete Observer – the Window Object in our example- owns the Concrete Subject – the Progress Bar – and can communicate with it via calling its public methods. The normal Case
- When the Concrete Subject – the Progress Bar- wants to communicate with a Observer – the Window, it will Call its inherited method notify() – the mothod in the interface Subject-, the Notify() method will look for all observers and call their update() methods.
- The message has arrived to the Observer(s) – in our case, the Window Object. and we acheives the 2-way communication.
Java has provided the mechanism to make it easy for us to implement the observer pattern.The Subject and Observer are generic interfaces interfaces. So, we can write them only once and implement them in our actual code.
Java – since JDK 1.o- provided an interface called Observer that has only one abstract method called update(Observable o, Object arg).
Java also provided a class Observable that contains the APIs used to communicate with the classes implementing the Observer interface – i.e call their update() method-. The Most important APIs are:
Let’s see the following Java example that demonstrates how to use the Observer/Observable classes.
In this example we have 2 Classes: BigClass and Sub. Normally, the BigClass object has a member of class Sub. We will make the sub Object count from 1 to SIZe, and if it reaches a given number- say 4 – it will notify its owner class – the BigClass object- that it has reached the given number
//*********** BigClass.java ***************//
import java.util.Observable;
import java.util.Observer;</code>
public class BigClass implements Observer {
private Sub theSub = null;
public BigClass(Sub theSub) {
System.out.println("in the BC constr");
this.theSub = theSub;
}
public void startCount(){
theSub.startCount();
}
public void update(Observable obs, Object obj) {
if (obs == theSub) {
System.out.println("the value: "+theSub.getValue()+" was reached!");
}
}
}
//************** Sub.java **************//
import java.util.Observable;
class Sub extends Observable {
private int n = 0;
private final int SIZE = 100;
public Sub(int n) {
System.out.println("in the sub cons");
this.n = n;
}
public void setValue(int n) {
this.n = n;
}
public int getValue() {
return n;
}
void startCount() {
for (int counter = 0; counter < SIZE; counter++) {
if (counter == this.n) {
setChanged();
notifyObservers();
}//end if
}//end for loop
}//end method
}
and now, we will use them both in the main method
public class Main {
public Main() {
Sub theSubObject = new Sub(45);
BigClass theBigObject = new BigClass(theSubObject);
theSubObject.addObserver(theBigObject);
theSubObject.setValue(44);
theBigObject.startCount();
}
public static void main(String[] args) {
Main m = new Main();
}
}
look at the code; we created objects for both Classes BigCalss, Sub. then we Told the Sub object to set the BigClass Object as its Observer :
theSubObject.addObserver(theBigObject);
Then, we started Counting ….. now, if we stumbled upon the given value, the Sub object will notify its observer – the BigClass Object by following sequence:
setChanged();
notifyObservers();
this sequence will send the message to the observer Object – the BigClass in our example.
As you can see, it is very easy to implement the observer pattern using java.
A look at Gnu Compiler

Gnu Compiler is one of the strongest and most widely used Compilers today. it is known for its power, flexibility, portability and wide architecture support.
Simply; you can use Gnu Complier on any unix/Linux, Windows or Mac.
you can also compile programs for a wide range of platform, including intel, AMD, ARM, PowerPC, SPARC …etc
Gnu Compiler support Languages like C, C++ (g++), Objective-C, fortran (gfortran), java (gcj), Ada , VHDL ……. use your imagination!!
So, let’s take a look at the architecture of the gnu compiler collection
Gnu Compiler is divided into 3 main components:
- front-end.
- middle-end.
- back-end.
We will take a closer look at theses three Layers:
- The Front-end:
This layer is Language-specific, architecture-neutral to handle the source code file.
Depending on the programming Language used to write the code , we choose the appropriate fornt-end.
for example: for C++ source files we use g++, for java files we use gcj …..
The purpose of the front end is to read the source file, parse it, and convert it into the standard abstract syntax tree (AST) representation. The AST is a dual-type representation: it is a tree where a node can have children and a list of statements where nodes are chained one after another.
INPUT : the source code , a text file contains the program to be compiled.
OUTPUT: a tree-like representation of the source code. Also called the Abstract Syntax Tree (AST).
- The Middle-end:
Language-neutral, architecture-neutral layer for enhancing/optimizing the tree representation of the source code (AST). and generating a register-transfer language (RTL) tree. RTL is a hardware-based representation that corresponds to an abstract target architecture with an infinite number of registers. So, RTL looks like Assembly code BUT IT ISN’T. it is more like architcture independent assembly. Code optimization is done in this stage, eliminating dead code – code can’t be executed – replacing some segements of code – the RTL – with faster segments and so on.
INPUT : Abstract Syntax Tree (AST).
OUTPUT: Register Transfer Language (RTL).
- The back-end:
A Language-neutral, yet Architecture-specific layer that generates the assembly code for the target architecture using the RTL representation -from the previous phase. the back-end stage has a description file for each hardware architecture that includes information about what’s available of the processor – the actual registers and so on. these descriptions are used to map the RTL instructions to the actual and appropriate assembly segment. As you can see, this phase tells us why gnu compilera are flixible, efficient and portable. for each chip/family, you can have a description file that tells the compiler WHAT availbale and how to use it. So, the compiler generates a very efficient assembly code for each processor chip.
INPUT : the RTL
OUTPUT: the Object file – the executable file
the following figure explains the output of each component :

the output of each compoenet of the gcc : 1)AST 2)RTL 3)Object Code
The Great Amarok !
![]()
Among all the media player i have used in both Linux and Windows, Amarok is my Number one Music player. I really like its interface and ease of use.
the new amarok 2 which comes with the new KDE 4 desktop environment makes my experience even more exciting.
the main features i like most about amarok are:
- Last.fm integration : i can listen to last.fm online music without having to use my browser, I just have to insert my login info Only ONCE to amarok, and Amarok will login automatically every time i use my last.fm playlist, import my favourites and play them. I can also through amarok, ban unwanted tracks, mark others as my favourites and even skip other songs.

amarok - last.fm integration
- System Tray control : this feature is common in all the media players that are part of the KDE4 desktop, if you want to jump to the next Track, and the player is Docked to the system tray, just RIGHT click amarok’s system tray icon, and you will find the shortcuts you just need : play/pause, net track previous track … etc

amarok - System tray Shortcuts
- The Lyrics applet : Yes, you can view the lyrics of the track while it is playing , when you run a track, Amarok will automatically fetch its lyrics from the internet for you!

amarok lyrics applet
- The Wikipedia applet : WoW ! while you are listening to anoe track, again Amarok, the Great will go to wikipedia and fetch the wiki page of the songer/band of the song !!!!!

amarok wikipedia applet
- The script Manager: Amarok was built to support plugins < the same idea of firefox, you can extend, enhance and add new third party features to Amarok > . Amarok script manager allows you to activate/deactivate scripts/applets < like the lyrics applet we just talked about > install/uninstall scriprs and download new ones.
Don’t You Agree with me that Amarok is a GREAT audio player ??? So, I must say I LOVE KDE4 !!
Hello world!
hello there !!!
welcome to my new WordPress blog
Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!