I am new to Java and was looking into implementing a simple Publish-Subscribe pattern using **only** J2SE and not use any other libraries.
Since i am pretty new to java, all i have done till now is write 3 classes
Main:
public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Subscriber s = new Subscriber(); Publisher p = new Publisher(); p.subscribe(s); } }
Publisher class:
public class Publisher { /** * @param args */ ArrayList<Subscriber> subscribers = new ArrayList(); public void subscribe(Subscriber s) { subscribers.add(s); } public void unsubscriber(Subscriber s) { subscribers.remove(s); } public void Notify() { for(int i = 0; i< subscribers.size(); i++) subscribers.get(i).update(); } }
Subscriber class:
public class Subscriber { /** * @param args */ public void update() { System.out.println("here"); } }
I understand that they are basic classes but since most of resources which i have found on the web all point to JMS i wasnt able to move forward and have come here.
Any pointers on moving forward would be helpful.
here's some additional info on how the program could work.
*Suppose the following events are specified in the input:*
Subscribe,John,ComputerScience
Subscribe,Sam,Physics
Publish,Dover,Mathematics
Publish,PrenticeHall,Literature
Subscribe,Sam,Literature
Publish,Dover,Physics
Subscribe,Mary,ComputerScience
Publish,PrenticeHall,ComputerScience
*Then the following results can be expected:*
Sam notified of a Physics book from Dover
John notified of a ComputerScience book from PrenticeHall
Mary notified of a ComputerScience book from PrenticeHall
I asked this question even in stack overflow but i never received any answer so i thought i could find some pointers here .. thank you for your time.