且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

您如何在不忙于等待的情况下暂停线程直到条件变为真?

更新时间:2023-09-20 18:33:46

潜在的问题是您使用 getter 方法获取信息并对其进行操作.此处违反的原则是告诉,不要问!.

The underlaying problem is that you use a getter method to aquire an information and act on it. The principle which is violated here is Tell, don't ask!.

OO解决此问题的方法是引入接口(例如:PlayerMovingListener).

The OO whay to solve this is to introduce an interface (e.g.: PlayerMovingListener).

player类将包含实现该接口的对象的实例(或它们的列表).播放器一开始走动,就会调用由播放器所拥有的(或所有)侦听器对象实现的接口中定义的isMoving()方法.
很可能是由当前Payer实现中的setter方法触发的.

The player class would hold an instance of an object implementing that interface (or a list of them). As soon as the player starts walking it calls the isMoving() method defined in the interface implemented by the (or all) listener object(s) held by the player.
Most likely is triggered by a setter method in your current Payer implementation.