方法链警告
Warning on method chains
老实说,我不确定我在寻找什么或如何提出它,但我试一试:
有时在包含方法链(如构建器)的 class 中,我们需要警告开发人员调用链末尾的方法才能生效,例如 android:
PreferenceManager.getDefaultSharedPreferences(this).edit();
如果在 Editor
中输入值后没有调用 commit()
或 apply()
,则会收到此警告:
SharedPreferences.edit() without a corresponding commit() or apply()
call.
我正在尝试为我的 class 实现类似的行为,我的意思是如果在链的末尾未调用特定方法则显示警告,任何线索或解决方案将不胜感激
谢谢
您要查找的是 Android 代码设计模式,称为 - Builder Design Pattern
。
Builder pattern is a creational design pattern it means its solves problem related to object creation.It is also very oftenly used in android development.
您在 SharedPreferences 示例中看到的是 Lint 检查,而不是 class 本身的行为。
您可以按照本指南 http://tools.android.com/tips/lint-custom-rules
在 Android Studio 中实施您自己的自定义检查
老实说,我不确定我在寻找什么或如何提出它,但我试一试:
有时在包含方法链(如构建器)的 class 中,我们需要警告开发人员调用链末尾的方法才能生效,例如 android:
PreferenceManager.getDefaultSharedPreferences(this).edit();
如果在 Editor
中输入值后没有调用 commit()
或 apply()
,则会收到此警告:
SharedPreferences.edit() without a corresponding commit() or apply() call.
我正在尝试为我的 class 实现类似的行为,我的意思是如果在链的末尾未调用特定方法则显示警告,任何线索或解决方案将不胜感激
谢谢
您要查找的是 Android 代码设计模式,称为 - Builder Design Pattern
。
Builder pattern is a creational design pattern it means its solves problem related to object creation.It is also very oftenly used in android development.
您在 SharedPreferences 示例中看到的是 Lint 检查,而不是 class 本身的行为。 您可以按照本指南 http://tools.android.com/tips/lint-custom-rules
在 Android Studio 中实施您自己的自定义检查