zoukankan      html  css  js  c++  java
  • C# 抽象类和接口的使用

    1.设计原则

    1.1 单一职责原则

    不要存在多于一个导致类变更的原因。通俗的说,即一个类只负责一项职责。(代码级别,方法级别,类的级别)

    1.2 里氏替换原则

    所有引用基类的地方必须能透明地使用其子类的对象。(不要重写父类的方法,不使用virtual关键字)

    1.3 依赖倒置原则

    高层模块不应该依赖低层模块,二者都应该依赖其抽象;抽象不应该依赖细节;细节应该依赖抽象。(多使用抽象类和接口) 

    1.4 接口隔离原则

    客户端不应该依赖它不需要的接口;一个类对另一个类的依赖应该建立在最小的接口上。(接口不要包含太多方法)         

    1.5 迪米特法则

    一个对象应该对其他对象保持最少的了解。(只与直接的朋友通信)出现成员变量、方法参数、方法返回值中的类为直接朋友关系,而出现在局部变量中的类则不是直接的朋友关系

    1.6 开闭原则

    一个软件实体如类、模块和函数应该对扩展开放,对修改关闭。

    2.类和类之间的关系

    组合,聚合,关联,依赖

     链接

    3.一个功能设计的演变

    3.1 组织信息导出

    public class OrganizationExcel
        {
            public void Export()
            {
                using (FileStream fs = new FileStream("d:/1.xlsx", FileMode.OpenOrCreate))
                {
                    string[] HeaderNameList = { "组织名称", "所属公司", "组织类型", "组织类别", "党委部室", "支部地址", "书记名" };
    
                    IWorkbook workbook = new XSSFWorkbook();
                    ICellStyle cellStyle = GetHeadCellStyle(workbook);
                    ISheet sheet = workbook.CreateSheet("sheet1");
                    IRow headerRow = sheet.CreateRow(0);
    
                    for (int i = 0; i < HeaderNameList.Length; i++)
                    {
                        ICell cell = headerRow.CreateCell(i);
                        cell.SetCellValue(HeaderNameList[i]);
                        cell.CellStyle = cellStyle;
                    }
    
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"组织{i}");
                        dataRow.CreateCell(1).SetCellValue($"公司{i}");
                        dataRow.CreateCell(2).SetCellValue($"类型{i}");
                        dataRow.CreateCell(3).SetCellValue($"类别{i}");
                        dataRow.CreateCell(4).SetCellValue($"党委部室{i}");
                        dataRow.CreateCell(5).SetCellValue($"支部地址{i}");
                        dataRow.CreateCell(6).SetCellValue($"书记名{i}");
                    }
    
                    workbook.Write(fs);
                }
            }
    
            protected ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.FillPattern = FillPattern.SolidForeground;
                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                return style;
            }
        }
    View Code

    3.2 增加对人员的导出

    public abstract class BaseExcel
        {
    
            /// <summary>
            /// 导出
            /// </summary>
            public void Export()
            {
                using (FileStream fs = new FileStream("d:/1.xlsx", FileMode.OpenOrCreate))
                {
                    IWorkbook workbook = new XSSFWorkbook();
                    ICellStyle cellStyle = GetHeadCellStyle(workbook);
                    ISheet sheet = workbook.CreateSheet("sheet1");
                    IRow headerRow = sheet.CreateRow(0);
    
                    for (int i = 0; i < HeaderNameList.Length; i++)
                    {
                        ICell cell = headerRow.CreateCell(i);
                        cell.SetCellValue(HeaderNameList[i]);
                        cell.CellStyle = cellStyle;
                    }
    
                    SetDataRow(sheet);
    
                    workbook.Write(fs);
                }
            }
            /// <summary>
            /// 表头数组
            /// </summary>
            public abstract string[] HeaderNameList { get; }
    
            /// <summary>
            /// 设置每一列的数据值
            /// </summary>
            public abstract Action<ISheet> SetDataRow { get; }
    
            protected ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.FillPattern = FillPattern.SolidForeground;
                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                return style;
            }
        }
    View Code
    class OrganizationExcel : BaseExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "组织名称", "所属公司", "组织类型", "组织类别", "党委部室", "支部地址", "书记名" }; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"组织{i}");
                        dataRow.CreateCell(1).SetCellValue($"公司{i}");
                        dataRow.CreateCell(2).SetCellValue($"类型{i}");
                        dataRow.CreateCell(3).SetCellValue($"类别{i}");
                        dataRow.CreateCell(4).SetCellValue($"党委部室{i}");
                        dataRow.CreateCell(5).SetCellValue($"支部地址{i}");
                        dataRow.CreateCell(6).SetCellValue($"书记名{i}");
                    }
                };
        }
    View Code
    class PersonnelExcel : BaseExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "人员名", "人员登录名", "人员组织", "人员类型"}; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"人员名{i}");
                        dataRow.CreateCell(1).SetCellValue($"人员登录名{i}");
                        dataRow.CreateCell(2).SetCellValue($"人员组织{i}");
                        dataRow.CreateCell(3).SetCellValue($"人员类型{i}");
                    }
                };
        }
    View Code

    3.3 增加对责任矩阵的excel导出

    public abstract class BaseExcel
        {
    
            /// <summary>
            /// 导出
            /// </summary>
            public void Export()
            {
                using (FileStream fs = new FileStream("d:/1.xlsx", FileMode.OpenOrCreate))
                {
                    var workbook = GetWorkBook("sheet1");
                    workbook.Write(fs);
                }
            }
            protected abstract IWorkbook GetWorkBook(string sheetName);
        }
    View Code
    public abstract class BaseOrdinaryExcel : BaseExcel
        {
            /// <summary>
            /// 表头数组
            /// </summary>
            public abstract string[] HeaderNameList { get; }
    
            /// <summary>
            /// 设置DataRow
            /// </summary>
            public abstract Action<ISheet> SetDataRow { get; }
    
            /// <summary>
            /// 获取excel对象
            /// </summary>
            /// <param name="sheetName">sheet名</param>
            /// <returns></returns>
            protected override IWorkbook GetWorkBook(string sheetName)
            {
                IWorkbook workbook = new XSSFWorkbook();
                ICellStyle cellStyle = GetHeadCellStyle(workbook);
                ISheet sheet = workbook.CreateSheet(sheetName);
                IRow headerRow = sheet.CreateRow(0);
    
                for (int i = 0; i < HeaderNameList.Length; i++)
                {
                    ICell cell = headerRow.CreateCell(i);
                    cell.SetCellValue(HeaderNameList[i]);
                    cell.CellStyle = cellStyle;
                }
    
                SetDataRow(sheet);
    
                return workbook;
            }
    
    
            protected ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.FillPattern = FillPattern.SolidForeground;
                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                return style;
            }
    
        }
    View Code
    class OrganizationExcel : BaseOrdinaryExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "组织名称", "所属公司", "组织类型", "组织类别", "党委部室", "支部地址", "书记名" }; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"组织{i}");
                        dataRow.CreateCell(1).SetCellValue($"公司{i}");
                        dataRow.CreateCell(2).SetCellValue($"类型{i}");
                        dataRow.CreateCell(3).SetCellValue($"类别{i}");
                        dataRow.CreateCell(4).SetCellValue($"党委部室{i}");
                        dataRow.CreateCell(5).SetCellValue($"支部地址{i}");
                        dataRow.CreateCell(6).SetCellValue($"书记名{i}");
                    }
                };
        }
    View Code
    class PersonnelExcel : BaseOrdinaryExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "人员名", "人员登录名", "人员组织", "人员类型"}; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"人员名{i}");
                        dataRow.CreateCell(1).SetCellValue($"人员登录名{i}");
                        dataRow.CreateCell(2).SetCellValue($"人员组织{i}");
                        dataRow.CreateCell(3).SetCellValue($"人员类型{i}");
                    }
                };
        }
    View Code
    public class ResponsibilityTableExcel : BaseExcel
        {
            ICellStyle headCellStyle;
    
            private const int
                ORGAN_ROW_NO = 0,               //组织行
                PERSONNEL_NAME_ROW_NO = 1;      //人员名行 
    
            protected override IWorkbook GetWorkBook(string sheetName)
            {
    
                IWorkbook workbook = new XSSFWorkbook();
                headCellStyle = GetHeadCellStyle(workbook);
                ISheet sheet = workbook.CreateSheet(sheetName);
    
                IRow organizationRow = sheet.CreateRow(ORGAN_ROW_NO);
                ICell cell = organizationRow.CreateCell(0);
                SetHeadValue(cell, "矩阵名");
                cell = organizationRow.CreateCell(1);
                SetHeadValue(cell, "组织名");
                sheet.AddMergedRegion(new CellRangeAddress(ORGAN_ROW_NO, ORGAN_ROW_NO, 1, 3));
    
                IRow personnelNameRow = sheet.CreateRow(PERSONNEL_NAME_ROW_NO);
                for (int i = 1; i < 4; i++)
                {
                    cell = personnelNameRow.CreateCell(i);
                    SetHeadValue(cell, $"人员{i}");
                }
                sheet.AddMergedRegion(new CellRangeAddress(ORGAN_ROW_NO, PERSONNEL_NAME_ROW_NO, 0, 0));
    
                return workbook;
            }
    
            private void SetHeadValue(ICell cell, string value)
            {
                cell.SetCellValue(value);
                cell.CellStyle = headCellStyle;
            }
    
            private ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.Alignment = HorizontalAlignment.Center;
                style.VerticalAlignment = VerticalAlignment.Center;
                return style;
            }
        }
    View Code

    3.4 抽象出接口

    public interface IExport
        {
            void Export();
        }
    View Code
        abstract class BaseExcel : IExport
        {
    
            /// <summary>
            /// 导出
            /// </summary>
            public void Export()
            {
                using (FileStream fs = new FileStream("d:/1.xlsx", FileMode.OpenOrCreate))
                {
                    var workbook = GetWorkBook("sheet1");
                    workbook.Write(fs);
                }
            }
            protected abstract IWorkbook GetWorkBook(string sheetName);
        }
    View Code
    abstract class BaseOrdinaryExcel : BaseExcel
        {
            /// <summary>
            /// 表头数组
            /// </summary>
            public abstract string[] HeaderNameList { get; }
    
            /// <summary>
            /// 设置DataRow
            /// </summary>
            public abstract Action<ISheet> SetDataRow { get; }
    
            /// <summary>
            /// 获取excel对象
            /// </summary>
            /// <param name="sheetName">sheet名</param>
            /// <returns></returns>
            protected override IWorkbook GetWorkBook(string sheetName)
            {
                IWorkbook workbook = new XSSFWorkbook();
                ICellStyle cellStyle = GetHeadCellStyle(workbook);
                ISheet sheet = workbook.CreateSheet(sheetName);
                IRow headerRow = sheet.CreateRow(0);
    
                for (int i = 0; i < HeaderNameList.Length; i++)
                {
                    ICell cell = headerRow.CreateCell(i);
                    cell.SetCellValue(HeaderNameList[i]);
                    cell.CellStyle = cellStyle;
                }
    
                SetDataRow(sheet);
    
                return workbook;
            }
    
    
            protected ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.FillPattern = FillPattern.SolidForeground;
                style.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey25Percent.Index;
                return style;
            }
    
        }
    View Code
     class OrganizationExcel : BaseOrdinaryExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "组织名称", "所属公司", "组织类型", "组织类别", "党委部室", "支部地址", "书记名" }; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"组织{i}");
                        dataRow.CreateCell(1).SetCellValue($"公司{i}");
                        dataRow.CreateCell(2).SetCellValue($"类型{i}");
                        dataRow.CreateCell(3).SetCellValue($"类别{i}");
                        dataRow.CreateCell(4).SetCellValue($"党委部室{i}");
                        dataRow.CreateCell(5).SetCellValue($"支部地址{i}");
                        dataRow.CreateCell(6).SetCellValue($"书记名{i}");
                    }
                };
        }
    View Code
        class PersonnelExcel : BaseOrdinaryExcel
        {
            public override string[] HeaderNameList { get { return new string[] { "人员名", "人员登录名", "人员组织", "人员类型"}; } }
    
            public override Action<ISheet> SetDataRow =>
                (sheet) =>
                {
                    for (int i = 1; i < 10; i++)
                    {
                        IRow dataRow = sheet.CreateRow(i);
                        dataRow.CreateCell(0).SetCellValue($"人员名{i}");
                        dataRow.CreateCell(1).SetCellValue($"人员登录名{i}");
                        dataRow.CreateCell(2).SetCellValue($"人员组织{i}");
                        dataRow.CreateCell(3).SetCellValue($"人员类型{i}");
                    }
                };
        }
    View Code
    class ResponsibilityTableExcel : BaseExcel
        {
            ICellStyle headCellStyle;
    
            private const int
                ORGAN_ROW_NO = 0,               //组织行
                PERSONNEL_NAME_ROW_NO = 1;      //人员名行 
    
            protected override IWorkbook GetWorkBook(string sheetName)
            {
    
                IWorkbook workbook = new XSSFWorkbook();
                headCellStyle = GetHeadCellStyle(workbook);
                ISheet sheet = workbook.CreateSheet(sheetName);
    
                IRow organizationRow = sheet.CreateRow(ORGAN_ROW_NO);
                ICell cell = organizationRow.CreateCell(0);
                SetHeadValue(cell, "矩阵名");
                cell = organizationRow.CreateCell(1);
                SetHeadValue(cell, "组织名");
                sheet.AddMergedRegion(new CellRangeAddress(ORGAN_ROW_NO, ORGAN_ROW_NO, 1, 3));
    
                IRow personnelNameRow = sheet.CreateRow(PERSONNEL_NAME_ROW_NO);
                for (int i = 1; i < 4; i++)
                {
                    cell = personnelNameRow.CreateCell(i);
                    SetHeadValue(cell, $"人员{i}");
                }
                sheet.AddMergedRegion(new CellRangeAddress(ORGAN_ROW_NO, PERSONNEL_NAME_ROW_NO, 0, 0));
    
                return workbook;
            }
    
            private void SetHeadValue(ICell cell, string value)
            {
                cell.SetCellValue(value);
                cell.CellStyle = headCellStyle;
            }
    
            private ICellStyle GetHeadCellStyle(IWorkbook workbook)
            {
                ICellStyle style = workbook.CreateCellStyle();
                style.Alignment = HorizontalAlignment.Center;
                style.VerticalAlignment = VerticalAlignment.Center;
                return style;
            }
        }
    View Code
    class OrganizationTxt : IExport
        {
            public void Export()
            {
                FileStream fs = new FileStream("d:/1.txt", FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
    
                string[] header = new string[] { "组织名称", "所属公司", "组织类型", "组织类别", "党委部室", "支部地址", "书记名" };
                sw.WriteLine(string.Join(" ", header));
    
                List<string> list = new List<string>();
                for (int i = 1; i < 10; i++)
                {
                    list.Add($"组织{i}");
                    list.Add($"公司{i}");
                    list.Add($"类型{i}");
                    list.Add($"类别{i}");
                    list.Add($"党委部室{i}");
                    list.Add($"支部地址{i}");
                    list.Add($"书记名{i}");
                    sw.WriteLine(string.Join(" ", list));
                    list.Clear();
                }
    
                sw.Flush();
                //关闭流
                sw.Close();
                fs.Close();
            }
        }
    View Code

    3.5 结论

    在这种设计中实现类既可以继承实现YourServiceBase ,也可以直接实现IYourService,提供了很好的灵活性。

  • 相关阅读:
    Bootstrap 2.2.2 的新特性
    Apache POI 3.9 发布,性能显著提升
    SQL Relay 0.48 发布,数据库中继器
    ProjectForge 4.2.0 发布,项目管理系统
    红帽企业 Linux 发布 6.4 Beta 版本
    红薯 快速的 MySQL 本地和远程密码破解
    MariaDB 宣布成立基金会
    Percona XtraBackup 2.0.4 发布
    Rocks 6.1 发布,光盘机群解决方案
    精通Servlet研究,HttpServlet的实现追究
  • 原文地址:https://www.cnblogs.com/gamov/p/11136950.html
Copyright © 2011-2022 走看看