示例代碼
using System;
using System.Collections.Generic;
using System.Text;
namespace ObserverExample
{
class Program
{
static void Main(string[] args)
{
ButtonPager buttonPager = new ButtonPager();
ListPager listPager = new ListPager();
Control gridview = new GridVIEw();
Control label = new Label ();
buttonPager.changePageHandler += new Pager.ChangePageHandler(buttonPager.ChangePage);
buttonPager.changePageHandler += new Pager.ChangePageHandler(gridvIEw.ChangePage);
buttonPager.changePageHandler += new Pager.ChangePageHandler(label .ChangePage);
buttonPager.changePageHandler += new Pager.ChangePageHandler(listPager.ChangePage);
listPager.changePageHandler += new Pager.ChangePageHandler(buttonPager.ChangePage);
listPager.changePageHandler += new Pager.ChangePageHandler(gridvIEw.ChangePage);
listPager.changePageHandler += new Pager.ChangePageHandler(label .ChangePage);
listPager.changePageHandler += new Pager.ChangePageHandler(listPager.ChangePage);
buttonPager.NextPage();
Console.WriteLine();
buttonPager.NextPage();
Console.WriteLine();
buttonPager.NextPage();
Console.WriteLine();
buttonPager.PreviousPage();
Console.WriteLine();
buttonPager.PreviousPage();
Console.WriteLine();
listPager.SelectPage(2);
Console.WriteLine();
listPager.SelectPage(1);
Console.WriteLine();
listPager.SelectPage(0);
}
}
abstract class Pager
{
protected int pageIndex = 0;
public int PageIndex
{
get { return pageIndex; }
set { pageIndex = value; }
}
protected int pageCount = 3;
public int PageCount
{
get { return pageCount; }
}
public event ChangePageHandler changePageHandler;
public delegate void ChangePageHandler(Pager sender);
protected void ChangePage()
{
if (changePageHandler != null )
changePageHandler(this);
}
}
class ButtonPager : Pager, Control
{
public void NextPage()
{
if (pageIndex < pageCount - 1)
{
Console.WriteLine("Click NextPage Button...");
pageIndex++;
ChangePage();
}
}
public void PreviousPage()
{
if (pageIndex > 0)
{
Console.WriteLine("Click PreviousPage Button...");
pageIndex--;
ChangePage();
}
}
public void ChangePage(Pager sender)
{
base.pageIndex = sender.PageIndex;
if (pageIndex > 0 && pageIndex < pageCount - 1)
Console.WriteLine("<<Previous Next>>");
else if (pageIndex == 0)
Console.WriteLine("Next>>");
else
Console.WriteLine("<<Previous");
}
}
class ListPager : Pager, Control
{
public void SelectPage(int pageIndex)
{
if (pageIndex >= 0 && pageIndex < pageCount)
{
Console.WriteLine(string.Format("Click <{0}> Link...", pageIndex + 1));
base.pageIndex = pageIndex;
ChangePage();
}
}
public void ChangePage(Pager sender)
{
base.pageIndex = sender.PageIndex;
for (int i = 1; i <= pageCount; i++)
{
if (pageIndex + 1 == i)
Console.Write(string.Format(" <{0}> ", i));
else
Console.Write(string.Format(" {0} ", i));
}
Console.WriteLine();
}
}
interface Control
{
void ChangePage(Pager sender);
}
class GridVIEw : Control
{
public void ChangePage(Pager sender)
{
Console.WriteLine(string.Format("GridVIEw->Show data of page {0}", sender.PageIndex + 1));
}
}
class Label : Control
{
public void ChangePage(Pager sender)
{
Console.WriteLine(string.Format("Label .Text=[{0}/{1}]", sender.PageIndex + 1, sender.PageCount));
}
}
}