Tuesday, January 26, 2010

Observer Pattern | Advantage of it along with example

 

Well to be frank about his; I’m a newbie who doesn’t know about Design Patterns. I just came across this today while I was coding and one of my associates helped me with this pattern.

This example might be simple and even idiotic for those pro’s who are swimming in the Java world. This is for those beginners who need to know the spark in this concept ;)

One might think differently after going through this…. trust me… even if you don’t understand :P

Well I found info about this pattern over here, but I just took conceptual part from it.

Simply, the Observer pattern allows one object (the observer) to watch another (the subject). The Observer pattern allows the subject and observer to form a publish-subscribe relationship. Through the Observer pattern, observers can register to receive events from the subject. When the subject needs to inform its observers of an event, it simply sends the event to each observer.

For example, you might have a spreadsheet that has an underlying data model. Whenever the data model changes, the spreadsheet will need to update the spreadsheet screen and an embedded graph. In this example, the subject is the data model and the observers are the screen and graph. When the observers receive notification that the model has changes, they can update themselves.

This is what it had to say… ;)

Just another way,

Suppose you wanted the Panel to tell the frame to display a message box when a button has been pressed, the Panel will have to hold a reference to the frame class somewhere and then in its code call a method of the frame class when the button has been pressed. So now you have the frame holding a reference to the panel and a panel holding a reference to the frame which == tight coupling, which is a bad thing and can make it hard to debug or extend your program.

I think you got it…. If not comment it down…. :D

So, Let’s talk about business…

I will post regarding Anonymous classes ASAP…

This is a simple application which changes text of button in another panel. Text on button in second panel changes if an event occurs in first panel and vice versa.

Observer Pattern Example

Observer Pattern Example

Following is the code which explains this….

Main.java

package MyOwnListener;
/**
 * @author HUNTER
 * hunter4coding.blogspot.com
 */
 
public class Main {
 
public static void main(String[] args) {



new MightyFrame().setVisible(true);
}
}

MightyFrame.java

package MyOwnListener;
 
import java.awt.BorderLayout;
import javax.swing.*;
/**
 * @author HUNTER
 * hunter4coding.blogspot.com
 */
 
public class MightyFrame extends JFrame{
 
firstPanel firstSoldier = new firstPanel();
SecondPanel secondSoldier = new SecondPanel();
 
public MightyFrame(){
 
this.getContentPane().add(BorderLayout.NORTH, firstSoldier);
this.getContentPane().add(BorderLayout.CENTER, secondSoldier);



this.pack();
this.setLocation(300,200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
 



firstSoldier.addLordListener(new LordListener(){
 
public void playWithMe(String text) {



// You might want to share the resources 
// of the panel here in frame



//JOptionPane.showMessageDialog(MightyFrame.this, text);
 
if(secondSoldier.btnTwo.getText().equals("NO"))
secondSoldier.btnTwo.setText("YES");
else
secondSoldier.btnTwo.setText("NO");
 
}
});        
 
secondSoldier.addLordListener(new LordListener(){
 
public void playWithMe(String text) {
//JOptionPane.showMessageDialog(MightyFrame.this, text);



if(firstSoldier.btnOne.getText().equals("NO"))
firstSoldier.btnOne.setText("YES");
else
firstSoldier.btnOne.setText("NO");
}
});
}
}

firstPanel.java

package MyOwnListener;
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
 * @author HUNTER
 * hunter4coding.blogspot.com
 */
 
 
public class firstPanel extends JPanel{
 
JButton btnOne = new JButton("Yes");



public firstPanel(){
 
this.add(btnOne);        
 
// Adding ActionListener to 'btnOne'
btnOne.addActionListener(new ActionListener(){
 
public void actionPerformed(ActionEvent e) {
littleTestOne();
}
});



}
 
// Panel event occurs here
private void littleTestOne(){        
this.listener.playWithMe("U CLICKED " + btnOne.getText());
}
 
private LordListener listener;
 
public void addLordListener(LordListener listening){
this.listener = listening;
}



}

secondPanel.java

package MyOwnListener;
 
import java.awt.event.*;
import javax.swing.*;
/**
 * @author HUNTER 
 * hunter4coding.blogspot.com
 */
 
public class SecondPanel extends JPanel{
 
JButton btnTwo = new JButton("NO");
 
public SecondPanel(){
 
// Adding ActionListener to 'btnTwo'
btnTwo.addActionListener(new ActionListener(){
 
public void actionPerformed(ActionEvent e) {
littleTestTwo();
}
});
 
this.add(btnTwo);
}
 
// Panel event occurs here
private void littleTestTwo(){
this.listener.playWithMe("U CLICKED " + btnTwo.getText());
}
 
private LordListener listener;
 
public void addLordListener(LordListener listening){
this.listener = listening;
}
 
}

 

LordListener.java

package MyOwnListener;
/**
 * @author HUNTER 
 * hunter4coding.blogspot.com
 */
 
public interface LordListener {
public void playWithMe(String text);
}

2 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 8 online training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    Hibernate Online Training Hibernate Online Training Spring Online Training Spring Online Training Spring Batch Training Online Spring Batch Training Online

    ReplyDelete