zoukankan      html  css  js  c++  java
  • C#设计模式系列 6 State 状态模式 之电视36计,我要自己掌握遥控器

       1.理论定义

           状态模式  允许 一个对象在其内部 状态改变时, 改变它的行为。

       2.应用举例

              需求描述: 暑期在家里看电视, 正值伦敦奥运会, 今天是星期六,在家里可以偷着乐,看看电视。

                             冰箱开一瓶 王老吉,坐在沙发上,拿起遥控器,一躺, 宅男的一天,又开始了。

                             这么多电视台,提供不同节目,该会是个充实的星期六。

                             大屏幕高清电视打开,我控制遥控器,在各个频道之间傲游.

       3.具体编码

        1.电视节目

        

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// 节目
        /// </summary>
        public  class Item
        {
            /// <summary>
            /// 节目开始时间
            /// </summary>
            public DateTime Start { get; set; }
            /// <summary>
            /// 节目结束时间
            /// </summary>
            public DateTime End { get; set; }
            /// <summary>
            /// 节目名
            /// </summary>
            public string Name { get; set; }
        }
    }

    2.每个频道 对应的  电视节目单

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// 电视节目单
        /// </summary>
        public class TVSchedule
        {
            /// <summary>
            /// 电视频道名字
            /// </summary>
            public string ChannelName { get; set; }
    
            /// <summary>
            /// 该电视频道 的 节目单
            /// </summary>
            public List<Item> Schedule { get;set;}
        }
    }

    3. 电视频道接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// 电视频道接口
        /// </summary>
        public interface IChannel
        {/// <summary>
            /// 电视节目表
            /// </summary>
             TVSchedule TVSchedule { get; set; }
        }
    }

    4.电视机

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        public class TV
        {
            /// <summary>
            /// 当前播放电视频道
            /// </summary>
            public IChannel Current { get; set; }
    
            /// <summary>
            /// 播放电视
            /// </summary>
            public void Play() {
               TVSchedule  Schedule =  Current.TVSchedule;
               Console.WriteLine("现在的电视台是:  "+Schedule.ChannelName);
               Console.WriteLine("电视节目单如下:  ");
               foreach (var i in Schedule.Schedule) {
                   string start =i.Start.ToString("HH:mm");
                   string end = i.End.ToString("HH:mm");
                   string name = i.Name;
                   Console.WriteLine(start+"--"+end+" 播放 :"+name);
               }
               Console.WriteLine("-------------------------------------------------");
            }
        }
    }

    5.湖南电视台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// 湖南电视台
        /// </summary>
        public class HuNan : IChannel
        {
          /// <summary>
            /// 电视节目表
            /// </summary>
            public TVSchedule TVSchedule { get; set; }
    
        }
    }

    6.CCTV-5 体育

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// CCTV-5 体育
        /// </summary>
        public class CCTV5 : IChannel
        {
          /// <summary>
            /// 电视节目表
            /// </summary>
            public TVSchedule TVSchedule { get; set; }
    
        }
    }

    7. CCTV-10 科教

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Com.Design.Gof.State
    {
        /// <summary>
        /// CCTV-10 科教
        /// </summary>
        public class CCTV10 : IChannel
        {
           /// <summary>
            /// 电视节目表
            /// </summary>
            public TVSchedule TVSchedule { get; set; }
    
        }
    }

    8.主函数调用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Com.Design.Gof.State;
    namespace Com.Design.Gof.Test
    {
        class Program
        {
           
            static void Main(string[] args)
            {
                // 1.打开电视,默认电视台是湖南卫视
                TV tv = new TV();
                HuNan hunan = new HuNan {TVSchedule = new TVSchedule
                { 
                  ChannelName = "湖南卫视",
                  Schedule = new List<Item> { 
                  new Item{ Name="我们约会吧(重播)", Start=DateTime.Parse("2012-08-04 00:00"), End=DateTime.Parse("2012-08-04 02:00")},
                  new Item{ Name="向上吧!少年    ", Start=DateTime.Parse("2012-08-04 02:00"), End=DateTime.Parse("2012-08-04 03:00")},
                  new Item{ Name="湖南新闻联播", Start=DateTime.Parse("2012-08-04 07:00"), End=DateTime.Parse("2012-08-04 07:30")},
                  new Item{ Name="我们约会吧", Start=DateTime.Parse("2012-08-04 07:30"), End=DateTime.Parse("2012-08-04 08:30")},
                  new Item{ Name="电视剧:还珠格格Ⅲ(37)", Start=DateTime.Parse("2012-08-04 08:30"), End=DateTime.Parse("2012-08-04 09:20")},
                  new Item{ Name="电视剧:还珠格格Ⅲ(38)", Start=DateTime.Parse("2012-08-04 09:27"), End=DateTime.Parse("2012-08-04 10:25")},
                } } };
                tv.Current = hunan;
                tv.Play();
                //现在遥控器 切换 到  CCTV5 体育频道
                CCTV5 cctv5 = new CCTV5{TVSchedule = new TVSchedule
                {
                  ChannelName = "CCTV5",
                  Schedule = new List<Item> { 
                  new Item{ Name="录像:女子射箭团体决赛", Start=DateTime.Parse("2012-08-04 01:45"), End=DateTime.Parse("2012-08-04 02:50")},
                  new Item{ Name="直播:男篮预赛(阿根廷-立陶宛)", Start=DateTime.Parse("2012-08-04 05:15"), End=DateTime.Parse("2012-08-04 07:00")},
                  new Item{ Name="早安奥林匹克", Start=DateTime.Parse("2012-08-04 07:00"), End=DateTime.Parse("2012-08-04 07:30")},
                  new Item{ Name="奥运风云会", Start=DateTime.Parse("2012-08-04 08:30"), End=DateTime.Parse("2012-08-04 09:00")},
                  new Item{ Name="录像:男子举重56公斤级决赛", Start=DateTime.Parse("2012-08-04 09:00"), End=DateTime.Parse("2012-08-04 11:00")},
                  new Item{ Name="奥运赛事集锦", Start=DateTime.Parse("2012-08-04 11:00"), End=DateTime.Parse("2012-08-04 12:25")},
                 } }};
                tv.Current = cctv5;
                tv.Play();
                //现在遥控器 切换 到  CCTV10 科教
                CCTV10 cctv10 = new CCTV10{  TVSchedule = new TVSchedule
                {
                  ChannelName = "CCTV10",
                  Schedule = new List<Item> { 
                  new Item{ Name="百家讲坛", Start=DateTime.Parse("2012-08-04 12:40"), End=DateTime.Parse("2012-08-04 14:10")},
                  new Item{ Name="动物探秘I1", Start=DateTime.Parse("2012-08-04 14:25"), End=DateTime.Parse("2012-08-04 15:00")},
                  new Item{ Name="欣赏", Start=DateTime.Parse("2012-08-04 15:07"), End=DateTime.Parse("2012-08-04 16:00")},
                  new Item{ Name="奥秘Ⅱ38", Start=DateTime.Parse("2012-08-04 15:12"), End=DateTime.Parse("2012-08-04 16:50")},
                  new Item{ Name="探索·发现", Start=DateTime.Parse("2012-08-04 16:00"), End=DateTime.Parse("2012-08-04 16:40")},
                  new Item{ Name="自然传奇", Start=DateTime.Parse("2012-08-04 16:45"), End=DateTime.Parse("2012-08-04 17:25")},
                  new Item{ Name="茶叶之路22", Start=DateTime.Parse("2012-08-04 20:30"), End=DateTime.Parse("2012-08-04 20:40")},
                  new Item{ Name="探索·发现", Start=DateTime.Parse("2012-08-04 20:40"), End=DateTime.Parse("2012-08-04 21:20")},
                 } } };
                tv.Current = cctv10;
                tv.Play();
                Console.ReadKey();
    
            }
    
        }
    }

    9.运行结果

    10.总结

      想用多线程来操作电视 Play,换台的时候  显示正在播放的电视。 感觉工程也比较大。

      所以就直接打印 电视节目单了。

      附件里面包括了程序源码。

      这里是附件下载(Download)

  • 相关阅读:
    hdu find the most comfortable road(贪心+并查集)
    hdu 迷瘴(贪心)
    Excel中指定的sheet名称或索引读取数据到DataTable
    SQLServer 批量插入数据的两种方法
    javascript基础教程3
    Enterprise Library 4.1学习笔记 配置应用程序块(c/s和b/s均适用)
    关于同一用户不能同时在两台电脑上登录问题的解决方案
    获取当天为一年中的第几周
    javascript基础教程4
    Excel导出方法选择(转自gwf25sz)
  • 原文地址:https://www.cnblogs.com/HCCZX/p/2622609.html
Copyright © 2011-2022 走看看