zoukankan      html  css  js  c++  java
  • 设计模式之策略模式

    策略模式是面向对象继承组合特性的使用。策略模式先设定出策略基类StrategyBase,进一步定义继承的子类。之后定义环境上下文Context类,内部根据不同的外部需求,寻找匹配StrategyBase基类的策略子类,通过环境控制Context类返回请求。

    下面是一个报告程序对模式进行了阐述:报告程序主要是根据不同的客户端请求打印预览不同的报告样式和数据。

    首先定义报告枚举:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    
    namespace CReport
    {
        /// <summary>
        /// 报告类型
        /// </summary>
        public enum ReportType
        {
            /// <summary>
            /// 身心减压放松0
            /// </summary>
            [Description("身心减压放松")]
            AuxiliaryHypnosisGuide = 0,
    
            /// <summary>
            /// 辅助催眠引导1
            /// </summary>
            [Description("辅助催眠引导")]
            BodyHeartDecompression = 1,
    
            /// <summary>
            /// 音乐放松训练3
            /// </summary>
            [Description("音乐放松训练")]
            MusicRelax = 2,
    
            /// <summary>
            /// 脑波牵引诱导3
            /// </summary>
            [Description("脑波牵引诱导")]
            BrainwaveInduction = 3
    
    
        }
    
    }
    View Code

     其次设定的报告基类是ReportClass,根据基类定义环境上下文Context类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using CrystalDecisions.Shared;
    using CrystalDecisions.ReportSource;
    using CrystalDecisions.CrystalReports.Engine;
    
    
    namespace CReport
    {
    
        public sealed class ReportContext
        {
    
            private ReportClass rs;
            public ReportContext(ReportType type)
            {
                switch (type)
                { 
                    case ReportType.BodyHeartDecompression:
                        rs = new BodyHeartDecompression();
                        break;
                    case ReportType.AuxiliaryHypnosisGuide:
                        rs = new AuxiliaryHypnosisGuide();
                        break;
                    case ReportType.MusicRelax:
                        rs = new MusicRelax();
                        break;
                    case ReportType.BrainwaveInduction:
                        rs = new BrainwaveInduction();
                        break;
                    default:
                        throw new NotImplementedException("无此报告类型");           
                
                }
            
            }
    
            public ReportClass ReturnReport(DataSet ds, DataTable dtPsl, DataTable dtbreath, DataTable dtHbRate, DataTable dttc) 
            {
                rs.SetDataSource(ds);
                rs.Subreports["脉搏曲线图"].SetDataSource(dtPsl);
                rs.Subreports["呼吸曲线图"].SetDataSource(dtbreath);
                rs.Subreports["综合放松度曲线图"].SetDataSource(dtHbRate);
                rs.Subreports["指标对比"].SetDataSource(dttc);
                return rs;     
                
            }
        
        }
    
    }
    View Code

    最后在客户端调用Context类请求报告

    1                 ReportContext Rc = new ReportContext((ReportType)Reportmode);
    2                 CrystalReportViewer rptViewer = new CrystalReportViewer();
    3                 rptViewer.DisplayGroupTree = false;
    4 
    5                 WindowsFormsHost host = new WindowsFormsHost();
    6                 rptViewer.ReportSource = Rc.ReturnReport(ds,dtPsl,dtbreath,dtHbRate,tcdt);
    7 
    8                 host.Child = rptViewer;
    9                 ReportGrid.Children.Add(host);
    View Code

    报告定义了四种类型报告和一些数据集表作为参数,返回报告ReportClass。

  • 相关阅读:
    用prototype属性来模拟一下类的继承
    Ajax 教程:Ajax 入门简介
    Ajax工作原理
    最新的Ajax教程和技术(上篇)
    javascript面向对象技术基础
    浏览器对象模型
    jQuery (选择器,属性,筛选,文档处理)
    shell(一)
    ntpntpdate时间同步
    centos7新系统安装
  • 原文地址:https://www.cnblogs.com/yshic/p/3108100.html
Copyright © 2011-2022 走看看