设计模式之策略模式

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

原文地址:https://www.cnblogs.com/yshic/p/3108100.html