桥接模式

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Data.SqlClient;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace DesignPattern.StructuralPattern
  9 {
 10     #region 桥接模式要点 重要
 11     //定义:将抽象部分和实现部分解耦,使他们能够独立变化,各自扩展互不影响
 12     //使用场景:系统需要在构件的抽象化角色和具体化角色之间添加更多的灵活性,避免在两个层次之间建立静态的联系。
 13     //应用:三层架构中的业务逻辑层BLL中通过桥接模式与数据操作层解耦(DAL),其实现方式就是在BLL层中引用了DAL层中一个引用
 14 
 15     //减少耦合就增加一层新的抽象
 16     #endregion
 17     public abstract class BridgePattern
 18     {
 19         public string Name { get; set; }
 20 
 21         public BridgePattern(string name)
 22         {
 23             Name = name;
 24         }
 25 
 26         AbstractDAL _abstractDAL = null;
 27         public AbstractDAL AbstractDAL 
 28         {   get { return _abstractDAL; }
 29             set { _abstractDAL = value; }
 30         }
 31        
 32         public BridgePattern(AbstractDAL dal)
 33         {
 34             _abstractDAL = dal;
 35         }
 36 
 37         public virtual bool Insert(string sql)
 38         {
 39             return _abstractDAL.Insert(sql);
 40         }
 41         public virtual bool Update(string sql)
 42         {
 43             return _abstractDAL.Update(sql);
 44         }
 45         public virtual bool Delete(string sql)
 46         {
 47             return _abstractDAL.Delete(sql);
 48         }
 49         public virtual bool Query(string sql)
 50         {
 51             return _abstractDAL.Query(sql);
 52         }
 53     }
 54 
 55     public class ConstructBridge : BridgePattern
 56     {
 57         public ConstructBridge(string name) : base(name)
 58         {
 59         }
 60         public override bool Query(string sql)
 61         {
 62             return base.Query(sql);
 63         }
 64     }
 65 
 66     /*****************************************具体实现部分********************************************/
 67 
 68     public abstract class AbstractDAL
 69     {
 70         public abstract bool Insert(string sql);
 71         public abstract bool Delete(string sql);
 72         public abstract bool Update(string sql);
 73         public abstract bool Query(string sql);
 74 
 75     }
 76 
 77     public class ConstructDAL : AbstractDAL
 78     {
 79         public ConstructDAL(string connection)
 80         {
 81 
 82         }
 83 
 84         public override bool Delete(string sql)
 85         {
 86             throw new NotImplementedException();
 87         }
 88 
 89         public override bool Insert(string sql)
 90         {
 91             throw new NotImplementedException();
 92         }
 93 
 94         public override bool Query(string sql)
 95         {
 96             throw new NotImplementedException();
 97         }
 98 
 99         public override bool Update(string sql)
100         {
101             throw new NotImplementedException();
102         }
103     }
104 }

 

posted on 2021-06-25 11:48  HowieGo  阅读(46)  评论(0)    收藏  举报

导航