- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我很想听听您在操作期间使用什么技术来验证对象的内部状态,从它自己的角度来看,只有内部状态不佳或不变破坏才会失败。
我的主要关注点是 C++,因为在 C# 中,官方和流行的方法是抛出异常,而在 C++ 中,不止一种单一方法可以做到这一点(好吧,在 C# 中不是这样要么,我知道)。
请注意,我不是在谈论函数参数验证,而更像是类不变完整性检查。
例如,假设我们想要一个 Printer
对象异步地Queue
打印作业。对于 Printer
的用户来说,该操作只能成功,因为异步队列结果会在另一个时间到达。因此,没有相关的错误代码可以传达给调用者。
但是对于 Printer
对象,如果内部状态不好,这个操作可能会失败,即类不变量被破坏,这基本上意味着:一个错误。 Printer
对象的用户不一定对这种情况感兴趣。
就个人而言,我倾向于混合使用三种内部状态验证方式,我无法真正确定哪一种是最好的(如果有的话),只能确定哪一种绝对是最差的。我想听听您对这些的看法,也希望您分享您对此事的任何经验和想法。
我使用的第一种风格 - 以可控的方式失败比损坏数据更好:
void Printer::Queue(const PrintJob& job)
{
// Validate the state in both release and debug builds.
// Never proceed with the queuing in a bad state.
if(!IsValidState())
{
throw InvalidOperationException();
}
// Continue with queuing, parameter checking, etc.
// Internal state is guaranteed to be good.
}
我使用的第二种风格 - 比损坏数据更好的崩溃无法控制:
void Printer::Queue(const PrintJob& job)
{
// Validate the state in debug builds only.
// Break into the debugger in debug builds.
// Always proceed with the queuing, also in a bad state.
DebugAssert(IsValidState());
// Continue with queuing, parameter checking, etc.
// Generally, behavior is now undefined, because of bad internal state.
// But, specifically, this often means an access violation when
// a NULL pointer is dereferenced, or something similar, and that crash will
// generate a dump file that can be used to find the error cause during
// testing before shipping the product.
}
我使用的第三种方式 - 比损坏的数据更好的静默和防御性救助:
void Printer::Queue(const PrintJob& job)
{
// Validate the state in both release and debug builds.
// Break into the debugger in debug builds.
// Never proceed with the queuing in a bad state.
// This object will likely never again succeed in queuing anything.
if(!IsValidState())
{
DebugBreak();
return;
}
// Continue with defenestration.
// Internal state is guaranteed to be good.
}
我对样式的评论:
Printer
对象上排队一样)。 您更喜欢这些中的任何一个,或者您有其他实现方法吗?
最佳答案
您可以将称为 NVI(非虚拟接口(interface))的技术与模板方法
模式结合使用。我大概是这样(当然,这只是我个人的看法,确实值得商榷):
class Printer {
public:
// checks invariant, and calls the actual queuing
void Queue(const PrintJob&);
private:
virtual void DoQueue(const PringJob&);
};
void Printer::Queue(const PrintJob& job) // not virtual
{
// Validate the state in both release and debug builds.
// Never proceed with the queuing in a bad state.
if(!IsValidState()) {
throw std::logic_error("Printer not ready");
}
// call virtual method DoQueue which does the job
DoQueue(job);
}
void Printer::DoQueue(const PrintJob& job) // virtual
{
// Do the actual Queuing. State is guaranteed to be valid.
}
因为 Queue
是非虚拟的,如果派生类覆盖 DoQueue
进行特殊处理,仍然会检查不变量。
关于您的选择:我认为这取决于您要检查的条件。
如果是内部不变量
If it is an invariant, it should not be possible for a user of your class to violate it. The class should care about its invariant itself. Therefor, i would
assert(CheckInvariant());
in such a case.
它只是一个方法的前置条件
If it's merely a pre-condition that the user of the class would have to guarantee (say, only printing after the printer is ready), i would throw
std::logic_error
as shown above.
我真的不鼓励检查条件,但什么都不做。
类的用户可以在调用方法之前自行断言它的先决条件已得到满足。所以一般来说,如果一个类负责某个状态,并且它发现一个状态无效,它应该断言。如果类(class)发现违反了不属于其责任的条件,则应抛出。
关于c# - 您如何验证对象的内部状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/343605/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!