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

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

 7InBlock.gif    public class DataServices
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public DataServices()
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            // 
12InBlock.gif            // TODO: Add constructor logic here
13InBlock.gif            //
14ExpandedSubBlockEnd.gif        }

15InBlock.gif        public static object LendRender(string strFile,BorrowReader borrower)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
17InBlock.gif                
18InBlock.gif            return borrower(strFile);
19ExpandedSubBlockEnd.gif        }

20InBlock.gif        public static object LendRender2(string strFile,IBorrowReader borrower)
21ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
22InBlock.gif                            
23InBlock.gif            return borrower.borrowReader(strFile);
24ExpandedSubBlockEnd.gif        }

25ExpandedSubBlockEnd.gif    }

26InBlock.gif    public interface IBorrowReader
27ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
28InBlock.gif        object borrowReader(string strFile);
29ExpandedSubBlockEnd.gif    }

30ExpandedBlockEnd.gif}

31 None.gif

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

InBlock.gif    class Class1
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

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

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

ExpandedSubBlockEnd.gif    }

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

InBlock.gif            
InBlock.gif            
return null;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
文件test.txt内容为:
ste str
abcdef
输出结果为:
----read by delegate
ste str
abcdef
ste str
abcdef
ste str
abcdef
----read by interface
ste str
abcdef

转载于:https://www.cnblogs.com/Finding2013/archive/2006/08/01/464910.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值