DesignPatternsMediator

CSharp · Console

/Program.cs

using System;
					
public class Program
{
		
	public abstract class DialogBox
	{
		public abstract void Changed(UIControl control);
	}

	
	public class UIControl
	{
		protected DialogBox owner;
		
		public UIControl(DialogBox owner)
		{
			this.owner = owner;
		}
	}
	
	public class ListBox: UIControl
	{
		private string selection;
		
		public string Selection
		{
			get {
				return selection;
			}
			set
			{
				selection = value;
				this.owner.Changed(this);
			}
		}
		
		public ListBox(DialogBox owner): base(owner)
		{
		}
	}
	
	public class TextBox: UIControl
	{
		private string content;
		
		public string Content
		{
			get {
				return content;
			}
			set
			{
				content = value;
				this.owner.Changed(this);
			}
		}
		
		public TextBox(DialogBox owner): base(owner)
		{
		}
	}
	
	public class Button: UIControl
	{
		private bool enabled;
		
		public bool Enabled
		{
			get {
				return enabled;
			}
			set
			{
				enabled = value;
				this.owner.Changed(this);
			}
		}
		
		public Button(DialogBox owner): base(owner)
		{
		}
	}
	
	public class ArticleDialogBox: DialogBox
	{
		ListBox articleListBox;
		TextBox titleTextBox;
		Button saveButton;
		
		public ArticleDialogBox()
		{
			articleListBox = new ListBox(this);
			titleTextBox = new TextBox(this);
			saveButton = new Button(this);
		}
		
		public override void Changed(UIControl control)
		{
			if(control is ListBox)
			{
				titleTextBox.Content = articleListBox.Selection;
				saveButton.Enabled = true;
			}
			else if(control is TextBox)
			{
				saveButton.Enabled = String.IsNullOrEmpty(titleTextBox.Content);
			}
		}
		
		public void SimulateUserInteraction()
		{
			articleListBox.Selection = "Article 1";
			Console.WriteLine("TextBox: " + titleTextBox.Content);
			Console.WriteLine("Button: " + saveButton.Enabled);
		}
	}
	
	
	public static void Main()
	{
		var dialog = new ArticleDialogBox();
		dialog.SimulateUserInteraction();
	}
}
;