Flags Over Objects

The Flags Over Objects anti-pattern occurs when behavior is written outside of an object by inspecting flags (such as status codes), rather than within the object itself.

This violates the Tell, Don’t Ask principle, and results in chatty interfaces and distribution of behavior that probably belongs within the object.

Working around this issue, one will often experience the Shotgun Surgery coding smell, in which many small changes to many files are required in order to make changes to the system.

 

There are many ways to address this anti-pattern.

In the worst cases, the State design pattern can effectively be used to bring all state-transition related behavior into the object, or at least into objects specifically constructed to handle the responsibility of state and state transitions.

 

posted on 2017-11-29 11:05  鲁传兵  阅读(103)  评论(0编辑  收藏  举报