Monday, August 17, 2009

Listener pattern implementation in java taught by Manju

//
// LISTENER PATTERN IMPLEMENTATION
// 1 NOTIFIER CLASS
// 3 RECEVIER CLASSES
// 2 EVENTS
//

import java.util.ArrayList;
import java.util.List;

public class ListenerImpl
{
public static void main(String[] args)
{
new ListenerImpl().run();
}

private void run()
{
// Base class ; sender of the message
Employee emp = new Employee("Ramesh", 24);

// Instantiate recevier classes that needs notification
Admin admin = new Admin();
Manager manager = new Manager();
CEO ceo = new CEO();

// NOTE: we could go ahead and add new classes for extension.

// add them all
emp.addEmployeeAttrChangeListener(admin);
emp.addEmployeeAttrChangeListener(manager);
emp.addEmployeeAttrChangeListener(ceo);

// Event 1
// notifies all classes
emp.setName("Manju");

System.out.println("\n\n");

// Event 2
// notifies all classes
emp.setAge(29);


}

class Employee
{
List listeners;
String name;
int age;

public Employee(String name, int age)
{
listeners = new ArrayList();
this.name = name;
this.age = age;
}

public void addEmployeeAttrChangeListener(EmployeeAttrChangeListener listener)
{
this.listeners.add(listener);
}

public void removeEmployeeAttrChangeListener(EmployeeAttrChangeListener listener)
{
this.listeners.remove(listener);
}

public void fireNameChangedEvent(String oldName, String newName)
{
for (EmployeeAttrChangeListener listener: listeners)
{
listener.nameChanged(oldName, newName);
}
}

public void fireAgeChangedEvent(int oldAge, int newAge)
{
for (EmployeeAttrChangeListener listener: listeners)
{
listener.ageChanged(oldAge, newAge);
}
}

public int getAge()
{
return age;
}

public void setAge(int age)
{
// notify and change
this.fireAgeChangedEvent(this.age, age);
this.age = age;

// NOTE : for notify after change swap statments
}

public String getName()
{
return name;
}

public void setName(String name)
{
// notify and change
fireNameChangedEvent(this.name, name);
this.name = name;

// NOTE : for notify after change swap statments
}
}
// Class A must implement appropriate listener interface.
class Admin implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In Admin block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In Admin block : Age change ");
System.out.println(oldAge + " has been changed to " + newAge);
}
}
// Class B must implement appropriate listener interface.
class Manager implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In Manager block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In Manager block : Age change");
System.out.println(oldAge + " has been changed to " + newAge);
}
}

// Class C must implement appropriate listener interface.
class CEO implements EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName)
{
System.out.println("In CEO block : Name change ");
System.out.println(oldName + " has been changed to " + newName);
}

public void ageChanged(int oldAge, int newAge)
{
System.out.println("In CEO block : Age change");
System.out.println(oldAge + " has been changed to " + newAge);
}
}


// Listener interface class ( in real world there would be marker implementations )

interface EmployeeAttrChangeListener
{
public void nameChanged(String oldName, String newName);

public void ageChanged(int oldAge, int newAge);
}
}


// SAMPLE OUTPUT
/*

In Admin block : Name change
Ramesh has been changed to Manju
In Manager block : Name change
Ramesh has been changed to
Manju
In CEO block : Name change
Ramesh has been changed to
Manju



In Admin block : Age change
24 has been changed to 29
In Manager block : Age change
24 has been changed to 29
In CEO block : Age change
24 has been changed to 29


*/

// Usage notes :
//
// Save entire file as ListenerImpl.java
// javac ListenerImpl.java
// java ListenerImpl

// The above example receiver classes are inner classes for sake of simplication
// It need not be the case in real situations.

Followers