首页 > 其他 > 详细

Design Pattern - Bridge Pattern

时间:2014-04-12 23:59:40      阅读:668      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
    class BridgePattern
    {
        public static void Main()
        {
            IReport reportMonthly = new MonthlyReport();
            IReport reportYearly = new YearlyReport();

            ReportChart reportChart = new ColumnReportChart();

            reportChart.SetReport(reportMonthly);
            reportChart.Display();

            reportChart.SetReport(reportYearly);
            reportChart.Display();
        }
    }

    abstract class ReportChart
    {
        protected IReport report;

        public void SetReport(IReport rpt)
        {
            report = rpt;
        }

        public abstract void Display();
    }

    class ColumnReportChart : ReportChart
    {
        public override void Display()
        {
            Console.WriteLine("------------{0} by ‘Column Chart!!‘-------------", report.ReportName);
            foreach (var item in report.GetReportData())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
        }
    }

    class PieReportChart : ReportChart
    {
        public override void Display()
        {
            Console.WriteLine("------------{0} by ‘Pie Chart!!‘-------------", report.ReportName);
            foreach (var item in report.GetReportData())
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
        }
    }

    interface IReport
    {
        string ReportName { get; }
        List<string> GetReportData();
    }

    class YearlyReport : IReport
    {
        public List<string> GetReportData()
        {
            var data = new List<string> 
            {
              "2010,370",
              "2011,420",
              "2012,320",
              "2013,490",
            };
            return data;
        }

        public string ReportName
        {
            get { return "Yearly Report"; }
        }
    }

    class MonthlyReport : IReport
    {
        public List<string> GetReportData()
        {
            var data = new List<string> 
            {
              "Jan,370",
              "Feb,420",
              "Mar,320",
              "Apr,490"
            };
            return data;
        }

        public string ReportName
        {
            get { return "Monthly Report"; }
        }
    }
bubuko.com,布布扣

 

Design Pattern - Bridge Pattern,布布扣,bubuko.com

Design Pattern - Bridge Pattern

原文:http://www.cnblogs.com/cnbwang/p/3660457.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!