WEB开发网
开发学院软件开发C语言 无废话C#设计模式之十九:Observer 阅读

无废话C#设计模式之十九:Observer

 2009-04-02 08:23:02 来源:WEB开发网   
核心提示: 如果今后页面上还需要针对分页功能有任何修改的话,真不知道怎么去改,无废话C#设计模式之十九:Observer(2),由此引入观察者模式来解决这些问题,示例代码using System;using System.Collections.Generic;using System.Text;na

如果今后页面上还需要针对分页功能有任何修改的话,真不知道怎么去改。由此引入观察者模式来解决这些问题。

示例代码

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 3 4 5 6  下一页

Tags:废话 设计模式 十九

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接