设计模式之策略模式

策略模式是面向对象继承组合特性的使用。策略模式先设定出策略基类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


    }

}


 

 其次设定的报告基类是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;     
            
        }
    
    }

}


 

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

            ReportContext Rc = new ReportContext((ReportType)Reportmode);
            CrystalReportViewer rptViewer = new CrystalReportViewer();
            rptViewer.DisplayGroupTree = false;

            WindowsFormsHost host = new WindowsFormsHost();
            rptViewer.ReportSource = Rc.ReturnReport(ds, dtPsl, dtbreath, dtHbRate, tcdt);

            host.Child = rptViewer;
            ReportGrid.Children.Add(host);


 

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

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值