各层的作用

  ①用户界面层:只负责显示和采集用户操作。

  ②业务逻辑层:负责UI和DAL层之间的数据交换,是系统架构中体现核心价值的部分。它关注点主要集中在业务规则的制定、业务流程的实现和业务需求的有关系统设计。它与系统所对应的领域(Domain)有关。也可以做一些如用户权限合法性和数据据式是否正确等验证检查。

  ③数据访问层:与数据库直接打交道,如数据库进行增、删、改、查等操作。常用的技术如ADO+SQL语句。这里可能还会有DBUtility类(负责建立数据库连接)、与工厂方法模式相关的DALFactory抽象类(用于创建如OracleDAL、SQLServerDAL对象之类的工厂)、DataAccess产品类(真正操作数据库的类)

各层的交互

  ①UI和BLL:UI层引用BLL和实体类(Entity)。

  ②BLL和DAL桥接模式,在BLL中持有一个DAL接口的引用

  ③DAL与数据库:工厂模式或抽象工厂模式。DAL与数据库间的解耦可参考《抽象工厂模式》部分的内容

//声明文件

//结构型模式:桥接模式
//场景:三层架构业务逻辑层(BLL)与数据操作层(DAL)的解耦#include <iostream>
#include <string>
#include <list>
using namespace std;//抽象的数据访问层(DAL)
class CAbsDataAccess{
public:virtual bool Insert(string name) = 0;virtual bool Delete(string name) = 0;virtual bool Update(string oldname, string newname) = 0;virtual string QueryByIndex(int iIndex) = 0;virtual void QueryAll() = 0;
};
//具体的数据访问层(DAL)
class CDataAccess : public CAbsDataAccess{
private:list<string> lstCustomer;
public:CDataAccess();~CDataAccess();bool Insert(string name);bool Delete(string name);bool Update(string oldname, string newname);string QueryByIndex(int iIndex);void QueryAll();
};//具体的业务逻辑类
class CCustomerBusiness{
private:CAbsDataAccess* pDAL;
public:CCustomerBusiness(CAbsDataAccess* dal);CCustomerBusiness(const CCustomerBusiness& other);void Add(string name);void Sub(string name);void Mod(string oldname, string newname);string GetBy(int index);void DispAll();
};

//实现文件

//具体的DAL层
CDataAccess::CDataAccess()
{//实际工程中从数据库中读取数据再填充列表lstCustomer.push_back("ZhangSan");lstCustomer.push_back("LiSi");lstCustomer.push_back("WangWu");lstCustomer.push_back("ZhaoLiu");
}
CDataAccess::~CDataAccess()
{cout << "Bofore deleting, size = " << lstCustomer.size() << endl;for(list<string>::iterator it = lstCustomer.begin(); it != lstCustomer.end();){it = lstCustomer.erase(it);}cout << "After deleted, size = " << lstCustomer.size() << endl;
}
bool CDataAccess::Insert(string name){lstCustomer.push_back(name); return true;}
bool CDataAccess::Delete(string name)
{bool bDeleted = false;for(list<string>::iterator it = lstCustomer.begin(); it != lstCustomer.end();it++){if((*it) == name){it = lstCustomer.erase(it); bDeleted = true; break;}}return bDeleted;
}
bool CDataAccess::Update(string oldname, string newname)
{bool bUpdate = false;for(list<string>::iterator it = lstCustomer.begin(); it != lstCustomer.end();it++){if((*it) == oldname){(*it) = newname; bUpdate = true; break;}}return bUpdate;
}
string CDataAccess::QueryByIndex(int iIndex)
{string strRet = "";if(iIndex < 0 || iIndex > lstCustomer.size() -1)       return strRet;list<string>::iterator it = lstCustomer.begin();for(int i = 0; i < iIndex; i++){it++;}strRet = (*it);return strRet;
}
void CDataAccess::QueryAll()
{cout << "***************************************************************" << endl;int i = 1;for(list<string>::iterator it = lstCustomer.begin(); it != lstCustomer.end(); it++, i++){cout << i << "        " << (*it) << endl;}cout << "***************************************************************" << endl << endl;
}
//具体的业务逻辑类
CCustomerBusiness::CCustomerBusiness(CAbsDataAccess* dal = NULL){pDAL = dal;}
CCustomerBusiness::CCustomerBusiness(const CCustomerBusiness& other){cout << "拷贝构造函数" << endl;pDAL = other.pDAL;
}
void CCustomerBusiness::Add(string name){pDAL->Insert(name);}
void CCustomerBusiness::Sub(string name){pDAL->Delete(name);}
void CCustomerBusiness::Mod(string oldname, string newname){pDAL->Update(oldname, newname);}
string CCustomerBusiness::GetBy(int index){return pDAL->QueryByIndex(index);}
void CCustomerBusiness::DispAll(){pDAL->QueryAll();}

//测试客户端

void main()
{CAbsDataAccess* pDAL = new CDataAccess();CCustomerBusiness oBLL(pDAL);oBLL.Add("Tony"); oBLL.Add("Mary"); oBLL.DispAll();oBLL.Mod("Tony", "ZhangSanfeng"); oBLL.Mod("Mary", "ZhouXingchi"); oBLL.DispAll();oBLL.Sub("ZhangSan"); oBLL.Sub("WangWu"); oBLL.DispAll();cout << "The first element in lst is " << oBLL.GetBy(0) << endl;CCustomerBusiness* pBLL = new CCustomerBusiness(oBLL);if(pBLL != NULL){pBLL->DispAll(); delete pBLL; }delete pDAL;
}

桥接模式的应用之三层架构中的业务逻辑层(BLL)与数据访问层(DAL)的解耦相关推荐

  1. 三层架构:表示层-业务逻辑-数据层

    三层架构:表示层-业务逻辑- 原文地址:三层架构:表示层-业务逻辑层-数据访问层  作者:灰烬 三层架构和MVC是两个东西. 非要相关的话: 三层架构中"表现层"的aspx页面对应 ...

  2. 三层架构:表示层-业务逻辑…

    原文地址:三层架构:表示层-业务逻辑层-数据访问层作者:灰烬 三层架构和MVC是两个东西. 非要相关的话: 三层架构中"表现层"的aspx页面对应MVC中的View(继承的类不一样 ...

  3. 三层架构:表示层-业务逻辑层-数据访问层

    三层架构和MVC是两个东西. 非要相关的话: 三层架构中"表现层"的aspx页面对应MVC中的View(继承的类不一样) 三层架构中"表现层"的aspx.cs页 ...

  4. 三层架构:表示层-业务逻辑层-数据访问层2

    概述 在软件体系架构设计中,分层式结构是最常见,也是最重要的一种结构.微软推荐的分层式结构一般分为三层,从下至上分别为:数据访问层.业务逻辑层(又或称为领域层).表示层. 三层结构原理: 3个层次中, ...

  5. java中表示层 控制层 业务逻辑层 数据访问层

    控制层(controller):的职能是负责读取视图表现层的数据,控制用户的输入,并调用业务层的方法: 业务层(service):需要根据系统的实际业务需求进行逻辑代码的编写,有些业务逻辑需要通过与数 ...

  6. 项目架构开发:数据访问层之Cache

    数据访问层简单介绍 数据访问层,提供整个项目的数据访问与持久化功能.在分层系统中所有有关数据访问.检索.持久化的任务,最终都将在这一层完成. 来看一个比较经典的数据访问层结构图 大概可以看出如下信息 ...

  7. java框架三层架构是_MVC框架模式和Javaweb经典三层架构

    一.MVC设计模式 首先我们需要知道MVC模式并不是javaweb项目中独有的,MVC是一种软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Co ...

  8. 【转】谈谈三层架构中MODEL的作用

    Model又叫实体类,这个东西,大家可能觉得不好分层.包括我以前在内,是这样理解的:UI<-->Model<-->BLL<-->Model<-->DAL ...

  9. 浅谈三层架构中的实体类(C#)

    最近因为三层架构中的实体类,引发了不少小问题,下面列举一下,谈谈自己的感想. 本文所指的实体类仅限于三层中的实体类,即数据库表的映射. 一.为什么要用实体类? |  使程序简洁易懂,便于维护. |  ...

最新文章

  1. 【工业互联网】全球工业互联网十大最具成长性技术展望(2019-2020年)
  2. MySQL 5.7 create VIEW or FUNCTION or PROCEDURE
  3. Lucky Substrings
  4. 单例模式可以分为懒汉式和饿汉式:     懒汉式单例模式:在类加载时不初始化。     饿汉式单例模式:在类加载时就完成了初始化,所以类加载比较慢,但获取对象的速度快。
  5. cmd命令操作Mysql数据库,命令行操作Mysql
  6. R语言观察日志(part11)--好用的R命令之高效安装
  7. 国科大高级人工智能3-DNN(BM/RBM/DBN/DBM)
  8. 为什么资源文件名以类型开头
  9. webrequest HttpWebRequest webclient/HttpClient
  10. 推荐30款最佳的数据可视化工具
  11. arm-linux 交叉编译toolchain
  12. 记录:GoogleMap 谷歌地图经纬度转屏幕坐标z,y,偏移量
  13. 获取当前的时间是第几周
  14. 不吹不黑!逛 GitHub 没看过这 10 个开源项目,绝对血亏...
  15. 曾经大肆其道的电商返利APP,其运营策略你真的清楚吗,一文带你读懂返利APP的竞品分析
  16. 高通android q 通过backtrace,使用addr2ine工具,定位crash问题记录
  17. Linux嵌入式设备U盘挂载和拷贝文件
  18. 期货开户加一分高交返靠谱吗?
  19. 网络搭建与应用2022国赛环境无误版
  20. plt.plot()的使用

热门文章

  1. 【转载】前端CSS规范整理
  2. java连接Access数据库的简单介绍
  3. 论文阅读【检测】CVPR2020 | Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Trai
  4. Caption Anything来了!可以解读清明上河图,细粒度可控的图像描述!
  5. 亚马逊对战阿里云 云计算行业上演大对决
  6. Camera2 openCamera
  7. Python | 变量的引用
  8. 鼠标移动到上面变小手和鼠標其他效果
  9. 自动化测试生成用例一之单个参数
  10. python3入门之集合set