Observer Patern Benfits

Using the observer pattern to reduce coupling between CameraRaycaster with its user classes, simplifies many other possible changes.

For example, perhaps it becomes necessary to change CameraRaycaster to check for hits in LateUpdate when GameObject positions are stable, rather than in Update when they are are still being updated.

In the current design, this change would create a race condition in CursorAffordance and PlayerMovement. These classes use LateUpdate to check CameraRaycaster.layerHit which could be invoked before CameraRaycaster.LateUpdate has updated the value. Restructuring to use the observor pattern isolates the responsibility for providing stable hit determination to CameraRaycaster, and enables user classes to do their thing without this concern.

The observer pattern would also simplify other optimizations like reducing calls to PlayerMovement.MouseMovement and ThirdPersonCharacter.Move when using indirect movement mode.

1 Like

BTW, I love the introduction of this pattern and patterns in general in the context of a concrete problem.

This pattern seems like it would have made Laser Defender so much easier.

1 Like

Privacy & Terms