[翻译]模块化编程:一种不使用类的方法第一部分

328阅读 0评论2008-11-19 X7rRKZQ
分类:

 
模块化编程:一种不使用类的方法第一部分
作者: 
译者:

译者序:这是一篇特殊的文章,它提供一种不使用类的模块化编程方法,对大部分开发者来说,也许并不值得关注,个人觉得,这篇文章适合那些已经习惯了class的存在,但却被迫在无class的平台(比如纯C)工作的程序员。

原文链接:

这是写给谁的?

这个系列是为那些认为C++的类是得不偿失的人写的。我打算为类的某些特性提供可以选择的传统一些的实现。这不是一篇讨论稿,所以我不解释为什么不使用类,我假设你已经阅读了很多说明类的缺陷的材料并且决定尽可能的避免使用它。这个系列文章的格式可能会和这一篇很像;首先提出一个我们可模拟的类的特性,然后提出实现的方法,紧接着是使用这种方法的限制条件。

对象实例

我曾经了解到基于类的编程对比传统的模块化编程的优点之一是允许存在对象的多个完全独立的对象而后者无法支持。这是这个系列的第一篇文章要推翻的话题。首先,我们看一下一个简单的class:

// let's say this is in c_example.h class C_Example { public:

void Displayxy( void ); void Setx( int xValue ); void Sety( int yValue ); private: int x; int y; }; // and this is in c_example.cpp #include "c_example.h" void C_Example::Displayxy( void ) { cout << "x == " << x << '\n' << "y == " << y << '\n'; } void C_Example::Setx( int xValue ) { x = xValue; } void C_Example::Sety( int yValue ) { y = yValue; }

现在, 声明和使用多个实例,看起来类似这样:

#include "c_example.h" void main( void ) { // create two instances of C_Example C_Example inst1; C_Example inst2; // set x and y for the first instance inst1.Setx( 5 ); inst1.Sety( 4 ); // set x and y for the second instance inst2.Setx( 8 ); inst2.Sety( 9 ); // display contents of each inst1.Displayxy(); inst2.Displayxy(); }

我想指出的是,这种方法极端愚蠢和无效, 我举这个例子以确保可以专心讨论我们的方法. 现在,看标准的模块版本:

// this might be in m_example.h void Example_Displayxy( void ); void Example_Setx( int xValue ); void Example_Sety( int yValue ); // and this might be in m_example.cpp #include "m_example.h" static int _x; static int _y; void Example_Displayxy( void )

{ cout << "x == " << _x << '\n' << "y == " << _y << '\n'; } void Example_Setx( int xValue ) { _x = xValue; } void Example_Sety( int yValue ) { _y = yValue; } // now to use the module: #include "m_example.h" void main( void ) { // you cannot declare multiple instances with this approach. // there is only one instance, which is the content of "m_example.cpp". Example_Setx( 5 ); Example_Sety( 4 ); Example_Displayxy(); }

现在有意思的地方到了。上面的例子明显的揭示了大部分人使用类作为唯一的支持多实例对象方法的原因。我要用的不同的方法使用数组容纳成员变量。

// the new "m_example.h" // the m_example data type typedef unsigned int M_Example; // module interface functions M_Example Example_CreateInstance( void ); void Example_Displayxy( M_Example objInst ); void Example_Setx( M_Example objInst, int xValue ); void Example_Sety( M_Example objInst, int yValue ); // the new "m_example.cpp" #include "m_example.h" #define _MAXOBJECTS 128 struct _PRIVATE { int x; int y; }static _private[ _MAXOBJECTS]; M_Example _curInstance = 0; M_Example Example_CreateInstance( void ) { _curInstance++; return( _curInstance - 1 ); } void Example_Displayxy( M_Example objInst ) { cout << "x == " << _private[objInst].x << '\n' << "y == " << _private[objInst].y << '\n'; } void Example_Setx( M_Example objInst, int xValue ) { _private[objInst].x = xValue; } void Example_Sety( M_Example objInst, int yValue ) { _private[objInst].y = yValue; } // and to use the new module: #include "m_example.h" void main( void ) { // with the new module you can create more than one instance: // create two instances of M_Example M_Example inst1 = Example_CreateInstance(); M_Example inst2 = Example_CreateInstance(); // set x and y for the first instance Example_Setx( inst1, 5 ); Example_Sety( inst1, 4 ); // then set x and y for the second instance Example_Setx( inst2, 8 ); Example_Sety( inst2, 9 ); // display the contents of both Example_Displayxy( inst1 ); Example_Displayxy( inst2 ); }

结论: 好的和不好的

正如你所看到的,无论你怎么认为,一个模块的多个实例是可行的。用这种方法,一个具有_private资格的结构体数组取代了关键字private,这里它是真正私有的,不会像在类定义里那样出现在接口列表中。数据类型M_Example其实只是一个int作为_private数组的索引ID。当你用Example_CreateInstance创建一个对象的时候,你只是保存了数组的一个位置。我确信你立刻就明白了这个方法的两个缺点。首先,对象的数量被限制在_MAXOBJECTS。其次,你可以创建一个对象,但是显然不能销毁它。好,那就用链表好了。我只使用数组是为了让思路清晰,但是你要使用这种方法,唯一的选择是链表。好,这就是要讨论的。任何问题,注解,热情,或者新的文章想法可以直接发送到。希望这篇文章能激发你的思维,如果有请告诉我。
.


--------------------next---------------------

上一篇:[翻译]良好设计的根基
下一篇:浅析Magic Number