接口到底能不能实例化?

 接口到底能不能实例话,在我以前看来是不行的,看了PetShop3后,感觉不是这样的。

数据访问层的DAL工厂:

namespace PetShop.DALFactory {
 
 public class Account
 {
  public static PetShop.IDAL.IAccount Create()
  {   
   /// Look up the DAL implementation we should be using
   string path = System.Configuration.ConfigurationSettings.AppSettings["WebDAL"];
   string className = path + ".Account";

   // Using the evidence given in the config file load the appropriate assembly and class
   return (PetShop.IDAL.IAccount) Assembly.Load(path).CreateInstance(className);
  }
 }
}

返回的是被转换为IAccount接口的实现了IAccount接口的对象,类似多态。我的理解是这样的:

比如某个类实现了该接口,如类Connection实现了该接口,则Connection类的

实例化对象可以给IConnection接口. 如 IConnection objIConnection = new Connection()

下面具体实现都基于接口进行操作。

在业务逻辑层,他是这么做的,符合上面的想法:

public class Account {
  
  public AccountInfo SignIn(string userId, string password) {

   // Validate input
   if ((userId.Trim() == string.Empty) || (password.Trim() == string.Empty))
    return null;

   // Get an instance of the account DAL using the DALFactory
   IAccount dal = PetShop.DALFactory.Account.Create();

   // Try to sign in with the given credentials
   AccountInfo account = dal.SignIn(userId, password);

   // Return the account
   return account;
  }

......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值