tortelee

导航

 

Definition

This is the definition from wikipedia.
In software systems, *encapsulate* refers to the bundling of data with the mechanisms or methods that operate on the data. It may also refer the limiting of direct access to some of that data, such as an objects' components.

Meaning

In object-oriented programming languages, and other relaetd fields, encapsulation refers to one of two related but distinct notions, and sometimes to the combination thereof:

  • A language mechanism for restricting direct access to some object's components
  • A language construct that facilitates the bundling of data with the methods operating on thos data.

the second defintion reflects that in many object -orient languages ,and other related fileds, the components are not hidden automatically and this can be overriden. Thus, information hidding is defined as a seperate notion by those who prefer the second definition.

对于我们讨论的封装,可能更在于第二个定义。

Encapsulation and inheritance

Inheritance often breaks encapsulation , given that inheritance expose a subclass to the details of its parents implementation. As described by the yo-yo problem, overuse of inheritance and therefore encapsulate, can become too compliated and hard to debug

Example

场景: 一个电商系统需要集成多个支付网关,如支付宝,微信,Paypal。 不同支付网关的API差异很大, 参数,签名方式,回调机制等。
问题: 如果直接在业务代码中调用不同支付网关的API, 会导致:

  • 业务逻辑和支付实现强耦合
  • 新增或更管支付网关需要修改多处代码
  • 支付逻辑的变动,如密钥更新,可能影响整个系统。

应用Encapsulate的解决方案:
1, 封装统一支付接口:

pulic interface PaymentService{
    PaymentResult pay(Order order);
    boolean refund(String orderId);
}

2,实现具体支付适配器

public class AlipayAdaper implements PaymentServices
{
@override
public PyamentResult pay(Order order)
{
    // 调用支付宝SDK, 处理参数转换,签名,加密等细节
}
}

public class WeChatPayAdapter implements PaymentServices
{
    @override
    public PaymentResult pay(Order order)
    {
        // 调用微信支付sdk, 处理XML格式请求,证书加载等
    }

}

3, 外部调用

PyamentService payment = PaymentFacotry.getService("alipay");
payment.pay(order)

效果:

  • 降低继承复杂度: 新增支付网关只需实现PaymentService接口,业务代码无需改动
  • 缺少依赖: 外部不感知支付宝或微信的具体实现细节。

封装的Other discussion

封装是一种策略,且是其他Intergrability策略的基础。他并不单独出现,经常和其他策略一起使用。

封装引入了明确的接口。接口包括API和它相应的职责(Responsiblity)。
封装也是比较常见的Modifiability策略,因此减少了 更改一个模块会传播到另外一个模块的概率。
耦合以前依赖于模块的内部,现在转移到了模块的接口。
现在外部的职责只能和模块暴露的接口交互。 接口,将容易的变化点进行抽象 , 即 接口会隐藏这些细节。

Reference

yo-yo problem

In software development, the yo-yo problem is an anti-pattern that occurs when a programmer has to read and understand a program whose inheritance graph is so long and complicated that the programmer has to keep flipping between many different class definitions in order to follow the control flow of the program. It is most often seen in the context of object-oriented programming. The term comes from comparing the bouncing attention of the programmer to the up-down movement of a toy yo-yo. Taenzer, Ganti, and Podar described the problem by name, explaining: "Often we get the feeling of riding a yoyo when we try to understand one of these message trees.

posted on 2025-05-05 15:29  tortelee  阅读(22)  评论(0)    收藏  举报