Publisher/Subscriber... practical benefits from event-driven patterns

Events are a critical component in Object Oriented programming. Whether they originate from user interaction or code logic, events are at the heart of what makes your application dynamic. Thanks to the advancement in both Java and .Net over the years, the event-driven paradigm has become possible on the web with the advent of JSF (Java Server Faces) and ASP.NET. Both of these technologies support the event-driven model allowing developers to take adantage of the "publisher/subscriber" pattern.

There are generally two types of events in Java and .NET; low-level events and semantic events.

Low-level events typically involve OS (GUI) type events such as Mouseclicks (onMouseClick, onMouseDown, etc) or keyboard actions (onKeyPress, onKeyDown, etc) whereas Semantic events are programmatic and involve the developer to create their own events, subsequently creating listeners to take custom actions based on those events. This article is geared toward utilizing semantic events.

What's the benefit of coding for semantic events?

That's a good question... let's take a typical application scenario and see how we can use events to develop a "real-world" solution.

Let's suppose we have an website that allows new users to register themselves (the event publisher). There are certain actions that need to happen when a new user has successfully completed the registration form (the event subscribers):

  • The database needs to be updated with the user's information.
  • The website owner needs to be notified that a user has registered and will need to approve the user before they can actually use the site.
  • The new user will need to be sent a "welcome to our site" type message.
  • The site will need to set a cookie in the user's browser so they will be "recognized" the next time they visit the site.

There are several ways to architect a suitable coding solution to meet these requirements, however by utilizing a publisher/subscriber design pattern we can create a scalable solution that will be flexible enough to handle any new actions that may be required at a later time. This is accomplished by firing an event when the user has registered. This will send out a notification to all subscribers of the "new user registration" event by way of a "callback" method. Each callback method would then be executed to perform their own specific tasks related to the new user registration. With this design in place, it becomes very easy to add new actions that may be required in the future by simply registering new callback methods to the "new user registration" event.

Java + .Net Demo 

Let's walk through a simple "Team Roster" demo to see the publisher/subscriber pattern in action. Here are the requirements for our application:

  • The application must be able to add new players to a team roster.
  • When a new player is added, we need to notify the coaching staff and the media
  • The application also must allow for new entities to be able to subscribe to new player notifications.

When reviewing the Java and C# code below you will notice that the code is very similar (same total lines of code in fact). One large difference is in the way the callback methods are handled. Java uses a single interface that all subscribing classes implement whereas .Net utilizes "Delegates."

NOTE: Delegates are similar to a "type-safe" function pointer in C/C++.

The "Team" class is considered the publisher in our demo. It contains the logic to actually "add" the player to the roster and then raises the event to notify the subscribers. The two listener classes (CoachListener and MediaListener) are then added to the event notfications. The "AddPlayerEvent" object is the actual class that is passed to the callback methods (with the new playername value).



   1:  using System;
   3:  namespace TeamRoster{
   5:      class AddPlayerEventArgs : EventArgs{ //create Event object
   6:          public string PlayerName { get; set; }
   7:      }
   8:      class CoachListener{
   9:          // subscribe callback method "NotifyCoachingStaff" to AddPlayerEvent (event) 
  10:          // using delegate "AddPlayerHandler" 
  11:          public void Subscribe(Team team){
  12:              team.AddPlayerEvent += new Team.AddPlayerHandler(NotifyCoachingStaff);
  13:          }
  14:          // create "Coach" Listener callback method
  15:          private void NotifyCoachingStaff(Team team, AddPlayerEventArgs e){
  16:              System.Console.WriteLine("To Coaching Staff: ({0}) was added to the team", e.PlayerName);
  17:          }
  18:      }
  19:      class MediaListener{
  20:          // subscribe callback method "NotifyMedia" to AddPlayerEvent (event) 
  21:          // using delegate "AddPlayerHandler" 
  22:          public void Subscribe(Team team){
  23:              team.AddPlayerEvent += new Team.AddPlayerHandler(NotifyMedia);
  24:          }
  25:          // create "Media" Listener callback method
  26:          private void NotifyMedia(Team team, AddPlayerEventArgs e){
  27:              System.Console.WriteLine("To Media Outlet: ({0}) was added to the team", e.PlayerName);
  28:          }
  29:      }
  30:      //publisher
  31:      class Team{
  32:          // delegate will hold a type-safe "pointer" to the callback 
  33:          // "Listener" callback methods
  34:          public delegate void AddPlayerHandler(Team team, AddPlayerEventArgs e);
  36:          public event AddPlayerHandler AddPlayerEvent; // event that callback methods will subscribe to
  38:          public void AddPlayer(string playername){ // method to add new player to roster
  39:              if (AddPlayerEvent != null){
  40:                  AddPlayerEventArgs e = new AddPlayerEventArgs();
  41:                  e.PlayerName = playername;
  42:                  System.Console.WriteLine("\nNew player added to the Roster, notifying all subscribers...");
  43:                  AddPlayerEvent(this, e); // call method to notify subscribers of new player
  44:              }
  45:          }
  46:      }
  47:      //main class
  48:      class AddTeamPlayer{
  49:          static void Main(){
  51:              Team team = new Team();
  52:              CoachListener coachlistener = new CoachListener();
  53:              MediaListener medialistener = new MediaListener();
  55:              coachlistener.Subscribe(team); // subscribe to event
  56:              medialistener.Subscribe(team); // subscribe to event
  58:              Console.Write("-Team Roster-\n");
  60:              team.AddPlayer("Memori"); // add player to roster
  61:              team.AddPlayer("Elias");
  62:              team.AddPlayer("Mikayli");
  63:              team.AddPlayer("Myranda");
  64:              team.AddPlayer("Elo");
  66:              Console.ReadLine();
  67:          }
  68:      }
  69:  }


 1 package TeamRoster;
 3 import java.util.*;
 5 class AddPlayerEvent extends EventObject { // Create event object
 6 	private String playername;
 7 	public String getPlayerName() { // getter method to return playername
 8 		return this.playername;
 9 	}
10 	// event constructor to set instance property
11 	public AddPlayerEvent(Object source, String playername) {
12 		super(source);
13 		this.playername = playername;
14 	}
15 }
16 // All listener classes will implement this interface
17 interface PlayerListener {
18 	void addPlayer(AddPlayerEvent addplayerevent);
19 }
20 // publisher
21 class Team {
22 	// method to add new player to roster
23 	public void AddPlayer(String playername) {
24 		System.out.println("\nNew player added to the Roster, notify all subscribers...");
25 		// call method to notify subscribers of new player
26 		OnAddPlayer(playername);
27 	}
28 	// method to notify all subscribers when a new player has been added
29 	// to the roster
30 	private void OnAddPlayer(String playername) {
31 		for (int i = 0, size = subscribers.size(); i < size; i++)
32 			((PlayerListener) subscribers.get(i)).addPlayer(new AddPlayerEvent(this, playername));
33 	}
34 	// type-safe ArrayList container to hold subscribers to the event
35 	List<PlayerListener> subscribers = new ArrayList<PlayerListener>();
36 	// method to add new subscribers to the ArrayList
37 	public void addPlayerListener(PlayerListener plistener) {
38 		subscribers.add(plistener);
39 	}
40 }
41 // main class
42 class AddTeamPlayer {
43 	public static void main(String[] args) {
44 		Team team = new Team();
45 		// create "Coach" PlayerListener callback class/method (Anonymous class)
46 		// approach
47 		team.addPlayerListener(new PlayerListener() {
48 			public void addPlayer(AddPlayerEvent e) {
49 				System.out.println("To Coaching Staff: (" + e.getPlayerName() + ") was added to the team");
50 			}
51 		});
52 		// create "Media" PlayerListener callback class/method (Inner class)
53 		// approach
54 		class MediaListener implements PlayerListener {
55 			public void addPlayer(AddPlayerEvent e) {
56 				System.out.println("To Media Outlet: (" + e.getPlayerName() + ") was added to the team");
57 			}
58 		}
59 		team.addPlayerListener(new MediaListener());
61 		System.out.print("-Team Roster-\n");
63 		team.AddPlayer("Memori"); // add player to roster
64 		team.AddPlayer("Elias");
65 		team.AddPlayer("Mikayli");
66 		team.AddPlayer("Myranda");
67 		team.AddPlayer("Elo");
68 	}
69 }

Here is the Console output from the .Net code:

Here is the Console output from the Java code:


posted on Friday, May 22, 2009 11:00 PM Print
# re: Publisher/Subscriber... practical benefits from event-driven patterns
1/10/2010 7:14 PM
nice example, cheers
# re: Publisher/Subscriber... practical benefits from event-driven patterns
energy suspension
6/22/2010 2:44 PM
say the even in qyestion was a query from a database like say a industry supplier like energy suspension bushings. would I be able to use this to bring a bout a nested array quesry fro cars and trucks perhaps? I think so aswesome!
# re: Publisher/Subscriber... practical benefits from event-driven patterns
6/2/2012 1:49 AM
It was very nice article... Can you explain me in detail.
# re: Publisher/Subscriber... practical benefits from event-driven patterns
10/14/2017 3:06 AM
Events are critical component in Object Oriented programming.
Spanish to English online lessons
bullet force games
# re: Publisher/Subscriber... practical benefits from event-driven patterns
thu hien
11/9/2017 2:27 AM
thank you, i really appreciate your posts watch free online Crackle
block noti group

# Brother Helpline Number
Angusa Carner
12/12/2017 5:10 AM
Brother Printer Helpline
# re: Publisher/Subscriber... practical benefits from event-driven patterns
aptoide apk
1/19/2018 1:05 AM
Aptoide app is one of the best platforms that allow users to download a plethora of applications easily.
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Ronaldo Chevy
1/26/2018 10:04 AM
This is very useful!
Double Glazing Glasgow
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Adam Smithhy
1/26/2018 10:32 AM
JSF 2.0 disadvantages? Honestly, apart from the relative steep learning curve when you don't have a solid background knowledge about basic Web Development (HTML/CSS/JS, server side versus client side, etc) and the basic Java Servlet API (request/response/session, forwarding/redirecting, etc), no serious disadvantages comes to mind. JSF in its current release still needs to get rid of the negative image it gained during the early ages, during which there were several serious disadvantages.

JSF 1.0 (March 2004)
This was the initial release. It was cluttered with bugs in both the core and performance areas you don't want to know about. Your webapplication didn't always work as you'd intuitively expect. You as developer would run hard away crying.

JSF 1.1 (May 2004)
This was the bugfix release.
Solicitors London
The performance was still not much improved. There was also one major disadvantage: you can't inline HTML in the JSF page flawlessly. All plain vanilla HTML get rendered before the JSF component tree. You need to wrap all plain vanilla in <f:verbatim> tags so that they get included in the JSF component tree. Although this was as per the specification, this has received a lot of criticism. See also a.o. JSF/Facelets: why is it not a good idea to mix JSF/Facelets with HTML tags?
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Natalie Smith
1/26/2018 10:33 AM
Rich Internet Applications are Web-based applications that function as traditional desktop applications however Web browsers (or clients) are required to access but unlike traditional applications. The key benefit RIAs possesses is the functionality to interact and manipulate with data, rather than simply visualize or represent it. And that’s the reason users demand their applications to be much more interactive, dynamic and highly responsive. It makes developer task more challenging to meet user expectation.
Photo booth hire Edinburgh
Market for the server-side languages is crowded and lot of languages are being used by the developers like JSF, ASP.NET, Adobe Flex, PHP and more which promises RIA features. So how can a new developer understand and realize that which application is best for his application. To find out the appropriate server side language always remains the hot issue for the companies and developers. For this reason I am going to compare the features of different frameworks.Two main technologies I am going to talk about are ASP.NET and JSF; both represent next generation development frameworks. Comparison will be based on ease of development provided to developer, tools for the framework and support to other frameworks.
# re: Publisher/Subscriber... practical benefits from event-driven patterns
David Burns
1/26/2018 10:35 AM
The term Web 2.0 is commonly associated with web applications that facilitate interactive information sharing, interoperability, user-centered design, and collaboration on the World Wide Web. To better understand Web 2.0 we can divide it into three sections:

RIA (Rich Internet Applications) – RIA helps to achieve the behavior of desktop application on web browser.
SOA (Service Oriented Architecture) – It is one of the key section of Web 2.0 that includes buzz words like Feeds, RSS, Web Services and Mash ups.
Social Web – Web 2.0 tends to interact much more with end user, end user is not only user of application but also a participant whether it is interacting with wiki or doing podcast or blogging.
What are Rich Internet Applications?
Rich Internet applications (RIAs) are web applications that have some of the characteristics of desktop applications, typically delivered by way of an Ajax framework, proprietary web browser plug-ins, advanced JavaScript compiler technology, or independently via sandboxes or virtual machines. Examples of RIA frameworks that require browser extensions include Adobe Flex, Java FX and Microsoft Silverlight. Rich Internet Applications serve users through web browser, via browser plugin or by an independent sandbox (Independent Desktop Application).
Skip hire Edinburgh

Rich internet applications use a distributed-function model rather than the simple thin-client–server model. RIAs enrich user experiences in part due to their reduced reliance on network/server communications.
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Julie roberts
1/26/2018 2:25 PM
I like this blog. It's very useful
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Julie roberts
1/26/2018 2:26 PM
We will look at four network applications, written completely from scratch in Java. We will see that we can write these programs without any knowledge of the technologies under the hood (which include operating system resources, routing between networks, address lookup, physical transmission media, etc.)

Each of these applications use the client-server paradigm, which is roughly

One program, called the server blocks waiting for a client to connect to it
A client connects
The server and the client exchange information until they're done
The client and the server both close their connection
Abogados de accidentes
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Julie roberts
1/26/2018 2:27 PM
The only pieces of background information you need are:

Hosts have ports, numbered from 0-65535. Servers listen on a port. Some port numbers are reserved so you can't use them when you write your own server.
Multiple clients can be communicating with a server on a given port. Each client connection is assigned a separate socket on that port.
Client applications get a port and a socket on the client machine when they connect successfully with a server.
Manchester Taxi
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Julie roberts
1/26/2018 2:28 PM
The four applications are

A trivial date server and client, illustrating simple one-way communication. The server sends data to the client only.
A capitalize server and client, illustrating two-way communication. Since the dialog between the client and server can comprise an unbounded number of messages back and forth, the server is threaded to service multiple clients efficiently.
A two-player tic tac toe game, illustrating a server that needs to keep track of the state of a game, and inform each client of it, so they can each update their own displays.
A multi-user chat application, in which a server must broadcast messages to all of its clients.
Carpet Cleaning Glasgow
# re: Publisher/Subscriber... practical benefits from event-driven patterns
Julie roberts
1/27/2018 6:22 AM
Java Development Kit (also known as JDK) contains the software and tools that you need to compile, debug, and run applets and applications that you've written using the Java programming language. JDK has as its primary components a collection of programming tools, including javac, jar, and the archiver, which packages related class libraries into a single JAR file. This tool also helps manage JAR files, javadoc - the documentation generator, which automatically generates documentation from source code comments, jdb - the debugger, jps - the process status tool, which displays process information for current Java processes, javap - the class file disassembler, and so many other components.

The JDK also comes with a complete Java Runtime Environment, usually called a private runtime. It consists of a Java Virtual Machine and all of the class libraries present in the production environment, as well as additional libraries only useful to developers, and such as the internationalization libraries and the IDL libraries.

Contents of the JDK:

Development Tools
(In the bin/ subdirectory) Tools and utilities that will help you develop, execute, debug, and document programs written in the JavaTM programming language.

Dynamic Launch SEO Glasgow

Post Comment

Title *
Name *
Comment *  
Coding strategies for the Java and .Net developer...