程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 無廢話C#設計模式之十九:Observer(2)

無廢話C#設計模式之十九:Observer(2)

編輯:關於C語言

示例代碼

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));

        }

    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved