《架构模式》阅读笔记 —— 经典的5种架构模型

引自:《Software Architecture Patterns

附脑图

分层架构

分层架构(layered architecture)是最常见的软件架构,也是事实上的标准架构。

  • 解耦方式:每一层都有清晰的角色和分工,而不需要知道其他层的细节。
  • 通讯方式:层与层之间通过接口通信

各层级的定义:

  1. 表现层(presentation):用户界面,负责视觉和用户互动
  2. 业务层(business):实现业务逻辑
  3. 持久层(persistence):提供数据,SQL 语句就放在这一层
  4. 数据库(database) :保存数据

So why not allow the presentation layer direct access to either the persistence layer or database layer? After all, direct database access from the presentation layer is much faster than going through a bunch of unnecessary layers just to retrieve or save database information. The answer to this question lies in a key concept known as layers of isolation.

The layers of isolation concept means that changes made in one layer of the architecture generally don’t impact or affect components in other layers: the change is isolated to the components within that layer, and possibly another associated layer (such as a persistence layer containing SQL).

The layers of isolation concept also means that each layer is independent of the other layers, thereby having little or no knowledge of the inner workings of other layers in the architecture.

层级隔离的设计理念,使得层与层之间实现了高内聚、低耦合。

Creating a services layer is usually a good idea in this case because architecturally it restricts access to the shared services to the business layer (and not the presentation layer). Without a separate layer, there is nothing architecturally that restricts the presentation layer from accessing these common services, making it difficult to govern this access restriction.

层级架构也使得增加新的业务层更加容易——因为层级(模块)间的耦合度很低,新的层级只需要处理层与层之间的接口就OK了。

示例

调整业务结构,我们增加一个新的层级——服务层。

The services layer in this case is marked as open,  meaning requests are allowed to bypass this open layer and go directly to the layer below it.

In the following example, since the services layer is open, the business layer is now allowed to bypass it and go directly to the persistence layer, which makes perfect sense.

由于新的层级是开放的而非闭合的,它允许上游层越过自身,实现直接对下游层数据的访问。

小结

分层架构可以直观的实现不同层级逻辑代码的解耦合,除此之外:

  • 结构简单,容易理解和开发
  • 不同技能的程序员可以分工,负责不同的层,天然适合大多数软件公司的组织架构
  • 每一层都可以独立测试,其他层的接口通过模拟解决

缺点:

  • 一旦环境变化,需要代码调整或增加功能时,通常比较繁杂和费时
  • 部署很麻烦:即使只修改一个小地方,往往需要整个软件重新部署,不容易做持续发布
  • 软件升级时,可能需要整个服务暂停
  • 扩展性差:用户请求大量增加时,必须依次扩展每一层,由于每一层内部是耦合的,扩展会很困难

模式分析:

  • 总体灵活性: 低
  • 发布易用性: 低
  • 可测试性: 高
  • 性能: 低
  • 规模扩展性: 低
  • 开发容易度: 高

In the following example, since theservices layer is open, the business layer is now allowed to bypass itand go directly to the persistence layer, which makes perfect sense.

猜你喜欢

转载自www.cnblogs.com/brt3/p/9824160.html
今日推荐