Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Reuse of iteration algorithms

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
5 Sep 20077 min read 52.3K   183   22  
Examples of decoupling iteration algorithm from actions on collection items
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;


namespace AlgorithmReuse
{
	public struct DisplayItem
	{
		public string Info;
		public string Message;
	}

	public class ProcessorBase
	{
		/// <summary>
		/// Messages from processors to show on UI.
		/// </summary>
		protected List<DisplayItem> m_DisplayItems = new List<DisplayItem>();

		/// <summary>
		/// Gets the messages to show on UI.
		/// </summary>
		public virtual List<DisplayItem> DisplayItems
		{
			get { return m_DisplayItems; }
		}
	}



	/// <summary>
	/// Processor to display all directories and all files.
	/// </summary>
	public class DisplayAllProcessor : ProcessorBase
	{
		#region Delegate properties

		public Iterators.DirectoryInfoHandler DisplayDirInfo
		{
			get { return MyDisplayDirInfo; }
		}

		public Iterators.FileInfoHandler DisplayFileInfo
		{
			get { return MyDisplayFileInfo; }
		}

		#endregion Delegate properties



		#region Functions returned by delegate properties

		private void MyDisplayDirInfo(DirectoryInfo Info)
		{
			DisplayItem di;
			di.Info = "Dir";
			di.Message = Info.FullName;
			m_DisplayItems.Add(di);
		}

		private void MyDisplayFileInfo(FileInfo Info)
		{
			DisplayItem di;
			di.Info = "File";
			di.Message = Info.FullName;
			m_DisplayItems.Add(di);
		}

		#endregion Functions returned by delegate properties
	}



	/// <summary>
	/// Processor to find an average and maximum file sizes in a collection.
	/// </summary>
	public class FindAvgAndMaxProcessor : ProcessorBase
	{
		private long m_MaxFileSize = 0;
		private FileInfo m_MaxFileInfo = null;
		private long m_TotalFileSizes = 0;
		private long m_TotalFiles = 0;


		public override List<DisplayItem> DisplayItems
		{
			get
			{
				m_DisplayItems.Clear();

				DisplayItem diAvg;
				diAvg.Info = "Avg file size";
				long lAverage = m_TotalFiles == 0 ? 0 : m_TotalFileSizes / m_TotalFiles;
				diAvg.Message = string.Format("{0} bytes", lAverage.ToString());
				DisplayItem diMax;
				diMax.Info = "Max file size";
				string strFileName = m_MaxFileInfo == null ? "None" : m_MaxFileInfo.FullName;
				diMax.Message = string.Format("File name: {0}, size: {1} bytes", strFileName, m_MaxFileSize);

				m_DisplayItems.Add(diAvg);
				m_DisplayItems.Add(diMax);

				return base.DisplayItems;
			}
		}

		protected virtual void ClearStats()
		{
			m_MaxFileSize = 0;
			m_MaxFileInfo = null;
			m_TotalFileSizes = 0;
			m_TotalFiles = 0;
		}


		#region Delegate properties

		public Iterators.FileInfoHandler CountFileSizes
		{
			get { return MyCountFileSizes; }
		}

		#endregion Delegate properties



		#region Functions returned by delegate properties

		protected virtual void MyCountFileSizes(FileInfo Info)
		{
			// Keep track of the biggest file.
			if (Info.Length > m_MaxFileSize)
			{
				m_MaxFileSize = Info.Length;
				m_MaxFileInfo = Info;
			}

			// Keep track of file count and the sum of all file sizes.
			++m_TotalFiles;
			m_TotalFileSizes += Info.Length;
		}

		#endregion Functions returned by delegate properties
	}



	public class FindAvgAndMaxByExtProcessor : FindAvgAndMaxProcessor
	{
		private string m_Extension;

		public FindAvgAndMaxByExtProcessor(string Extension)
		{
			m_Extension = Extension;
		}

		public string Extension
		{
			get { return m_Extension; }
			set
			{
				m_Extension = value;
				ClearStats();
			}
		}


		#region Functions returned by delegate properties

		protected override void MyCountFileSizes(FileInfo Info)
		{
			if (0 == string.Compare(Info.Extension, m_Extension, StringComparison.CurrentCultureIgnoreCase))
			{
				base.MyCountFileSizes(Info);
			}
		}

		#endregion Functions returned by delegate properties
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions