C.1: Organize related data into structures (structs or classes)
C.1:组织相关数据形成结构体或者类
Reason(原因)
Ease of comprehension. If data is related (for fundamental reasons), that fact should be reflected in code.
降低理解难度。如果数据(由于根本的原因)具有相关性,那么这个事实应该反映到代码上。
译者注:面向对象的软件设计首先是要对实现的目标系统建模。
Example(示例)
void draw(int x, int y, int x2, int y2); // BAD: unnecessary implicit relationships void draw(Point from, Point to); // better
Note(注意)
A simple class without virtual functions implies no space or time overhead.
不包含虚函数的简单类不会产生空间和时间方面的额外代价。
Note(注意)
From a language perspective class and struct differ only in the default visibility of their members.
从语言的观点来看,class和struct的区别只是默认状态下成员的可见性不通。
译者注:这只是编译层面的区别。
Enforcement(实施建议)
Probably impossible. Maybe a heuristic looking for data items used together is possible.
大概是不可能的。也许启发式地寻找一起使用的数据是可能的。
觉得本文有帮助?请分享给更多人。
更多更新文章,欢迎关注微信公众号【面向对象思考】
面向对象设计,面向对象编程,面向对象思考!