zoukankan      html  css  js  c++  java
  • 设计模式-桥接模式及使用场景

    官方定义:

    桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interfce)模式。

    最佳实践:

    如果一个系统需要在构建的抽象化角色和具体角色之间增加更多的灵活性,避免在两个层次之间建立静态的继承关系,通过桥接模式可以使它们在抽象层建立一个关联关系,抽象化角色和实现化角色可以独立扩展而互不影响,在程序运行时可以动态的组合。
    一个类存在两个维度的变化,且这两个维度都需要进行扩展

    案例:

    使用桥接模式将数据库选择的功能抽象出来,也就是上面说的抽象化角色(DB),DB和Repository之间有更多的灵活性,可以自由选择、独立扩展、互不影响,在程序运行时可以动态组合。
    DB:包含IDbConnection,子类目前有MysqlDB、SqlServerDB,还可以扩展其他数据库DB

    客户端:

                IDB orderDB = new MysqlDB("server=.;~~~~~~~");
                Order_masterRepository orderRepository = new Order_masterRepository(orderDB);
                var orderList = orderRepository.Select("SELECT * FROM Order_master");//从Mysql版的Order数据库查找订单
    
    
                IDB productDB = new SqlServerDB("server=.;~~~~~~~");
                ProductRepository productRepository = new ProductRepository(productDB);
                var productList = productRepository.Select("SELECT * FROM Product");//从Sqlserver版的Product数据库查找产品
    

    Model:

        public class Order_master
        {
            public int ID { get; set; }
            public decimal TotalPrice { get; set; }
        }
        public class Product
        {
            public int ID { get; set; }
            public string Title { get; set; }
        }
    

    DB:

        public interface IDB
        {
            IDbConnection GetConnection();
        }
        public class MysqlDB : IDB
        {
            private string _connStr = null;
            public MysqlDB(string connStr)
            {
                _connStr = connStr;
            }
            public IDbConnection GetConnection()
            {
                return new MySqlConnection(_connStr);
            }
        }
        public class SqlServerDB : IDB
        {
            private string _connStr = null;
            public SqlServerDB(string connStr)
            {
                _connStr = connStr;
            }
            public IDbConnection GetConnection()
            {
                return new SqlConnection(_connStr);
            }
        }
    

    Repository:

        public interface IRepository<T>
        {
            IEnumerable<T> Select(string sql);
        }
        public class Order_masterRepository : IRepository<Order_master>
        {
            private IDbConnection _connection = null;
            public Order_masterRepository(IDB db)
            {
                _connection = db.GetConnection();
            }
            public IEnumerable<Order_master> Select(string sql)
            {
    
                return _connection.Query<Order_master>(sql);
            }
        }
        public class ProductRepository : IRepository<Product>
        {
            private IDbConnection _connection = null;
            public ProductRepository(IDB db)
            {
                _connection = db.GetConnection();
            }
            public IEnumerable<Product> Select(string sql)
            {
                return _connection.Query<Product>(sql);
            }
        }
    
  • 相关阅读:
    guaguia
    webapp手机移动端开发技巧
    两个数组对象对比
    json 根据某个子,寻找父节点的算法
    递归写法
    数组归类
    视频点击按钮下载
    微信小程序 直接跳转到首页
    iframe 跨域传值
    判断对象是否为空
  • 原文地址:https://www.cnblogs.com/fanfan-90/p/13276966.html
Copyright © 2011-2022 走看看