知识库--面向对象编程的缺陷(The Deficiency of the Object Model)(118)

本文探讨了Java中对象模型的身份与状态绑定问题,并分析了这种绑定如何导致并发问题。作者指出,这种方式虽然简单易懂,但在多线程环境中会导致低并发性能。文章进一步讨论了现实世界中的对象行为与当前面向对象范式之间的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The Deficiency of the Object Model

// 身份和状态—-合并
As Java programmers, we are well versed in object-oriented programming(OOP). But the language has greatly infulenced the way we model OO apps. OOP did not quite turn out to be what Alan Kay had in mind when he coined the term. His vision(愿景) was primarily message passing, and he wanted to get rid of data. Somewhere along the way, OO languages started down the path of data hiding through Abstract Data Types(ADTs), binging data with procedure or combing state and behavior. This largely led us toward encapsulating and mutating state. In the process, we ended up merging the identity with state–the merging of the instance with its data.

This merging of identity and state snuck up on many Java programmers in a way that its consequences may not be obvious(悄然种入后果). When we traverse a pointer or reference to an instance, we land at the chunk of memory that holds its state. It feels natural to manipulate(操纵) the data at that location. The location represents the instance and what it contains. The combined identity and state worked out to be quite simple and easy to comprehend. However, this had some serious ramifications(分支) from the concurrency point of view.(从并发的角度就有问题了)

We will run into concurrency issues if, for example, we are running a report to print various details about a bank account–the number, current balance, transactions, minimum balance and so on. The reference(引用是入口) on hand is the gateway to the state that could change at any time. So, we are forced to lock other threads while we view the account. And the result is low concurrency. The problem did not start with the locking but with the merging of the account identity with its state.(身份和状态的绑定)

//OO 模拟现实世界还差点事
We were told that OO programming models the real word. Sadly, the real world does not quite behave as the current OO paradigm tries to model. In the real world, the state does not change, the identity does. We will discuss next how that is true.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值