Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / C#

Distributed Command Pattern - an extension of command pattern for connected systems

Rate me:
Please Sign up or sign in to vote.
4.87/5 (74 votes)
25 Jan 2005CPOL16 min read 262.5K   2.7K   252  
Distributed Command Pattern is a pattern for connected systems which implements command pattern. It frees designers from thinking about the communication and helps them concentrate on implementing commands as if it is a regular desktop application. The framework takes care of the communication.
using System;
using System.Threading;
using System.Collections;

namespace DistributedCommand.Framework
{
	/// <summary>
	/// Command Bus is the message bus which takes command and executes
	/// via command executors.
	/// </summary>
	public class CommandBus : ICommandExecutorListener
	{
		/// <summary>
		/// Pipeline contains the distributed executors which get invoked
		/// on every command execution
		/// </summary>
		private ArrayList _Pipeline = new ArrayList();

		/// <summary>
		/// Broadcast mode, set to true when any incoming command is also
		/// send to the pipeline in order to distribute to all distributed
		/// executors.
		/// </summary>
		private bool _BroadcastReceive = false;
	
		/// <summary>
		/// Initialize the command bus.
		/// 
		/// Command bus can either work as just transmitter or can work
		/// as command broadcast server.
		/// </summary>
		/// <param name="broadcastReceive">True for broadcast servers</param>
		public CommandBus( bool broadcastReceive )
		{
			this._BroadcastReceive = broadcastReceive;
		}

		/// <summary>
		/// Add a command executor in the bus pipeline
		/// </summary>
		/// <param name="executor">The executor which executes command</param>
		public void AddExecutor( ICommandExecutor executor )
		{
			this._Pipeline.Add( executor );
			executor.AddListener( this );
		}

		/// <summary>
		/// Remove the executor
		/// </summary>
		/// <param name="executor">The executor to remove</param>
		public void RemoveExecutor( ICommandExecutor executor )
		{
			this._Pipeline.Remove( executor );
		}

		/// <summary>
		/// Execute the specified command via all command executors 
		/// in the pipeline
		/// </summary>
		/// <param name="command">The command to execute</param>
		public void Execute( ICommand command )
		{
			foreach( ICommandExecutor executor in this._Pipeline )
				executor.Execute( command );
		}
		
		/// <summary>
		/// A command was received from any of the distributed executors.
		/// Excute the command on the pipeline
		/// </summary>
		/// <param name="command">Received command from a executor</param>
		void ICommandExecutorListener.Receive(ICommand command)
		{
			this.Execute( command );
		}

		/// <summary>
		/// Remove the executor from the pipeline.
		/// </summary>
		/// <param name="executor"></param>
		void ICommandExecutorListener.Remove( ICommandExecutor executor )
		{
			this._Pipeline.Remove( executor );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions