学习的点点滴滴

Email : 107311278@qq.com
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

模版方法模式

Posted on 2014-01-16 22:41  v薛定谔的猫v  阅读(90)  评论(0)    收藏  举报

说明:将共用部分抽成一个接口或者抽象方法. 比如我们操作数据库 增加 修改 删除定义一个抽象类。

类图

代码


public abstract class Database<T>
{
  public abstract List<T> Select(int id);
  public abstract int Add(T t);
  public abstract bool Update(T t);
  public abstract bool Delete(int id);
}

public class ModelADAL : Database<ModelA>
{
  public override List<ModelA> Select(int id)
  {
    throw new NotImplementedException();
  }

  public override int Add(ModelA t)
  {
    throw new NotImplementedException();
  }

  public override bool Update(ModelA t)
  {
    throw new NotImplementedException();
  }

  public override bool Delete(int id)
  {
    throw new NotImplementedException();
  }
}

public class ModelBDAL : Database<ModelB>
{
  public override List<ModelB> Select(int id)
  {
    throw new NotImplementedException();
  }

  public override int Add(ModelB t)
  {
    throw new NotImplementedException();
  }

  public override bool Update(ModelB t)
  {
    throw new NotImplementedException();
  }

  public override bool Delete(int id)
  {
    throw new NotImplementedException();
  }
}

public class ModelA
{
}

public class ModelB
{
}