Observer Design Pattern

I’ll talk now a little about the observer design pattern, or i can call it the listener design pattern.

the Observer design pattern define a one-to-many relationship between objects that when an object status changes the other dependent objects be notified.

the idea is : there is an object that has a peace of information, many other objects interested in knowing when its state changes.

the way to handle this is creating a way to allow dependent objects to register to be notified when the status changes. here is an imagination of how you can model this situation:

Class Diagram

Class Diagram

From the diagram you can see that we created to interfaces one for the subject object which will contains the information or status other objects needed to be aware about. the Subject interface has 3 methods registerObserver, deregisterObserver and notifyObservers.

registerObserver(): takes an Observer object as a argument and add this object to an internal arraylist or whatever you choose to keep all registered observers on it.

deregisterObserver(): takes an Observer object as a argument and and remove this object from the observers arraylist.

notifyObservers(): call the update() method on all observers exists on the observers arraylist and send the new status(s) as arguments.

The Observer Interface contains one method update()

update(): this method takes the status or information it interested in as argument(s).

so in pref. an subject object has information some observer objects interested in, so the observer objects register themselves on the subject object by calling the register method.

when the data in the subject object changed the notifyObservers method call and it send the new information by calling update method in all registered observers.

when an observer object decided to stop receiving updates it deregister himself from the subject object by calling deregister method() on the subject object.

Add comment July 13, 2008

ISD-Server could not start because port 5800 is not available

Hello every one,

few days ago my ubuntu machine started to give me this error every  time i login:

ISD-Server could not start because port 5800 is not available.

i google it but didn’t found a good solution but after while i found one from ubuntu forum:

ica is part of italc.sourceforge.net and it is installed on my machine when i installed edubuntu.

and to stop getting this error message here is what i did:

$sudo apt-get remove libitalc italc-client italc-master

and i never see the error message again.

here is the thread which i found the solution in:

http://ubuntuforums.org/showthread.php?t=749394&page=2

1 comment June 9, 2008

linux english to arabic dictionary

sometimes i needed for a desktop dictionary on ubuntu linux, now i found how to setup the arabic dictionary in the linux Dictionary:

for gnome :

- open Applications Menu -> Accessories -> Dictionary.

- the Dictionary application is opened. now click edit menu and select Preferences.

- in the source tab click Add button.

- A new window opened, in the Description field write: Arabic Dictionary. or whatever you want to name it.

- let Transport select box as Transport.

- Hostname : dict.arabeyes.org

- Port: 2628

now enjoy using the desktop dictionary.

2 comments May 10, 2008

install ubuntu from usb flash memory

now i’ll explain how to install ubuntu without using cdrom and with a usb flash memory:

a usb flash memory by default is formatted in vfat file system, i will not change the file system type.

- first you need to create a master boot record in the flash memory

$ sudo parted /dev/sdb set 1 boot on

you need to change sdb to you usb device, you can know what is the link to your device from the command mount

now you have a bootable usb storage device all what you need next is to copy the kernel and init parameters file in the suitable place in your flash memory.

- create a directory named casper in root of your flash memory.

- download the following files and put them in the directory named casper

1- http://archive.ubuntu.com/ubuntu/dists/hardy/main/installer-i386/current/images/hd-media/vmlinuz

2- http://archive.ubuntu.com/ubuntu/dists/hardy/main/installer-i386/current/images/hd-media/initrd.gz

- get the iso image which will be used to install ubuntu, you need to download the alternative CD not the normal cd.

- extract the iso image and copy the folder isolinux from the extracted iso to the root of usb flash memory.

- rename the isolinux folder in the root of the flash memory to syslinux.

- inside the syslinux folder rename the file isolinux.cfg to syslinux.cfg

- copy the alternative cd iso image -not the extracted folder- to the folder casper on the flash memory.

everything now is fine and you can reboot that machine and boot from the usb flash memory then install ubuntu.

enjoy.

Add comment May 10, 2008

ubuntu `Replaces’ field, invalid package name ` error

Today, when i was trying to install some packages i got this error:

dpkg: parse error, in file `/var/lib/dpkg/status’ near line 14938 package `human-theme’:
`Replaces’ field, invalid package name `
E: Sub-process /usr/bin/dpkg returned an error code (2)

i googled it to search for a know solution but got nothing useful this time. so i decieded to think a little bit and try to solve it my self. and here is the solution i got.

i open the file which through the parse error:

# gedit /var/lib/dpkg/status

then i visited the line which given to has the problem and found the name of package has some encoded characters so i removed the non understandable characters and saved the file.

then:

#apt-get -f update

every thing after that went ok. and i enjoyed my ubuntu again.

Add comment April 26, 2008

convert .flac to .mp3

Salam all,

this about how to convert a flac audio file to a mp3 format to gain the size reduce

you need to have the following packages installed: flac , lame

for ubuntu you can install them by:

$ sudo apt-get install flac lame

for defora

# yum install flac lame

then to convert a flac file to mp3

$ flac -cd filename.flac | lame -h - filename.mp3

Add comment April 6, 2008

for ibrahim: how to create jdbc connection to mysql

- first you need to get the jdbc driver and put it in your lib folder or in any place your build path.

- second use the next code:

Class.forName(”com.mysql.jdbc.Driver”).newInstance();

String url = “jdbc:mysql://hostname:3306/dbname”;

Connection con = DriverManager.getConnection(url,username,password);

Statement st = con.createStatement();

ResultSet rs = st.executeQuery(”your query here”);

while(rs.next())

{

System.out.println(rs.getString(”fieldname”));

}

con.close();

Add comment April 4, 2008

linux command screen

this post is bout how to use the screen command to connect multiusers to the same screen:

first you need to create a new screen and give it a name:

# screen -S screenName

if you want to have multi users able to connect to it, so you need to enable the multiuser mode:

press Ctrl+a

you will find screen command line appears at the bottom of the screen, then type

:multiuser on

and press enter

now you screen is running in multiuser mode and ready to accept other connection

to attach to this screen:

# screen -x screenName

Add comment April 1, 2008

create SVN Repository

to create a new SVN repository it is an easy task :

  • first you need to have the subversion installed in your machine :
  • fedora :

# yum install subversion

  • ubuntu :

# apt-get install subversion

  • now you have subversion installed and ready to create your repository:

# svnadmin create /path/to/repository/folder

Add comment March 29, 2008

Short circuit operators Vs. Non-short circuit operators

short circuit operators (&& , ||) differs from non-short circuit operators (& , |) in the way they deal with the right hand side expression.

the short circuit && evaluate the expression on the left hand side first, if it is false so there is no reason to evaluate the right hand side expression, because in all cases the final result will be false.

also the short circuit || evaluate the expression in the left hand side first, if it is true so there is no need to evaluate the right hand side because in all case the final result will be true.

so when using short circuit operators (&& , ||) you should expect that the right hand side expression may not be evaluated at all.

the non-short circuit operators (& , |) are less efficient than short circuit operators because they evaluate the right hand side even they knows that the evaluation of it will make no difference to the final result.

here is an example:

int x =3, y=8;

if(x < y || y++ < 9)

{

System.out.println(” the int y will never be incremented”);

}

the part y++ < 9 will not be evaluated so the int y will never be incremented because the left hand side of the && operator will be true so && operator will escape evaluating the right hand side.

Add comment November 19, 2007

Previous Posts


Categories

Links

Archives