• 一个C#中接口模式的例子


    BDataServices类中,LendRender应用一个委托BorrowReader实现对文件的读操作,LendRender2应用接口类实现文件读操作。
     1namespace datalayer
     2{
     3    public delegate object BorrowReader(string strPath);
     4    /// <summary>
     5    /// The class to provide data
     6    /// </summary>

     7    public class DataServices
     8    {
     9        public DataServices()
    10        {
    11            // 
    12            // TODO: Add constructor logic here
    13            //
    14        }

    15        public static object LendRender(string strFile,BorrowReader borrower)
    16        {
    17                
    18            return borrower(strFile);
    19        }

    20        public static object LendRender2(string strFile,IBorrowReader borrower)
    21        {
    22                            
    23            return borrower.borrowReader(strFile);
    24        }

    25    }

    26    public interface IBorrowReader
    27    {
    28        object borrowReader(string strFile);
    29    }

    30}

    31

    调用类,定义一个BorrowReader对象br,为br增加相应的代理实体函数。
    using System;
    using System.Data;
    using System.IO;
    namespace DP_Interface
    {
        
    /// <summary>
        
    /// Summary description for Class1.
        
    /// </summary>

        class Class1
        
    {
            
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>

            [STAThread]
            
    static void Main(string[] args)
            
    {
                
    //
                
    // TODO: Add code to start application here
                
    //
                Console.WriteLine("----read by delegate");
                datalayer.BorrowReader br;
                br
    =new datalayer.BorrowReader(readTxt);
                br
    +=new datalayer.BorrowReader(readTxt);
                br
    +=new datalayer.BorrowReader(readTxt);
                datalayer.DataServices.LendRender(
    "test.txt",br);
                Console.WriteLine(
    "----read by interface");
                datalayer.DataServices.LendRender2(
    "test.txt",new borrower());
                Console.Read();
                
            }

            
    public static object readTxt(string strPath)
            
    {
                StreamReader sr
    =new StreamReader(strPath,System.Text.Encoding.ASCII,false,8000);
                
    string strContent;
                
    while((strContent=sr.ReadLine())!=null)
                
    {
                    Console.WriteLine(strContent);
                }
                
                
    return null;
            }

        }

        
    class borrower:datalayer.IBorrowReader
        
    {
            
    public object borrowReader(string strFile)
            
    {
                StreamReader sr
    =new StreamReader(strFile,System.Text.Encoding.ASCII,false,8000);
                
    string strContent;
                
    while((strContent=sr.ReadLine())!=null)
                
    {
                    Console.WriteLine(strContent);
                }

                
                
    return null;

            }

        }

    }
    文件test.txt内容为:
    ste str
    abcdef
    输出结果为:
    ----read by delegate
    ste str
    abcdef
    ste str
    abcdef
    ste str
    abcdef
    ----read by interface
    ste str
    abcdef
  • 相关阅读:
    php判断字符串长度 strlen()与mb_strlen()函数
    Ajax
    MYSQL全文本搜索
    PHP读取xlsx Excel 文件
    PHP正则表达式
    语法环境 变量 数据类型 转换 销毁和传值
    语法环境 变量 数据类型 转换 销毁和传值
    hive并行执行作业; 强化在脑海的印象
    待解决问题 :JDBC indexInsert.addBatch(); 为什么不生效 PSTM
    Clickhouse副本表以及分布式表简单实践
  • 原文地址:https://www.cnblogs.com/Finding2013/p/464910.html
Copyright © 2020-2023  润新知