简体   繁体   English

课堂互动设计

[英]class interaction design

Lets say i have something like this coded 可以说我有这样的编码

class Normal_Mode;
class Fast_Mode;
class File_Control; //handles all operations with reading/writing in file
class Main_Control {
 private:
 some_class *root; //all other classes need access to root pointer since there is all the data(binary tree)
 File_Control *c_file_control;
 Fast_Mode *c_fast_mode;
...
}

Main_Control::Main_Control ( int argc, char* argv[]) {
  ...
  if ( argc > 1 ) {
   c_fast_mode = new Fast_Mode(argc, argv[]);
  } else {
   c_normal_mode = new Normal_Mode();
  };
  ...
};

int main (int argc, char* argv[]) {
 Main_Control c_main_control(argc,argv);
 return 0;
}

Lets say user input had argc > 1 and i am happy doing stuff with users input in Fast_Mode class but when i am finished and want to write stuff to file or read something from file while in Fast_Mode. 可以说用户输入的argc> 1,并且我很乐意使用Fast_Mode类中的用户输入来处理内容,但是当我完成操作后,想要在Fast_Mode中将内容写入文件或从文件中读取内容时,就可以这样做。 How do people in real world access File_control class? 现实世界中的人们如何访问File_control类?

Do they make some global array full with pointers to these kinda of classes who need only 1 instance. 它们是否使一些全局数组充满指向这些仅需要1个实例的类的指针。

Do they pass pointers to Fast_Mode and other classes so it can have it stored in private members for access. 他们是否将指针传递给Fast_Mode和其他类,以便可以将其存储在私有成员中以供访问。

or they construct/destruct such classes all the time depending on when it is needed. 或者他们始终根据需要的时间构造/销毁此类。

And what do they do with such *root pointer where all the actual data is stored and lot of other classes needs to access it 以及如何使用这样的* root指针来存储所有实际数据,而许多其他类需要访问它

Or my design ideas are completely wrong and people in real world do it some other way? 还是我的设计思路完全错误,现实世界中的人们以其他方式这样做吗?

I don't really know what you are trying to achieve with this: if possible make you goal more clear but what I would say is that people would ususally create an abstract interface called 'Mode' and then have both Normal_Mode and Fast_Mode implement it. 我真的不知道您要通过这个实现什么:如果可能的话,使您的目标更加明确,但是我要说的是,人们通常会创建一个称为“模式”的抽象接口,然后让Normal_Mode和Fast_Mode都实现它。 That way you could write the following code: 这样,您可以编写以下代码:

class Main_Control {
    private:
       some_class *root; //all other classes need access to root pointer since there is all the data(binary tree)
       File_Control *c_file_control;
       Mode *c_mode;
       ...
};

And then you could set it like: 然后可以将其设置为:

if ( argc > 1 ) {
   c_mode = new Fast_Mode(argc, argv[]);
} else {
   c_mode = new Normal_Mode();
};

So you just put the common functions in the Mode interface so that they behave the same way but can be placed inside the same mode variable. 因此,您只需将公共函数放在Mode接口中,以使它们的行为相同,但是可以将它们放在相同的mode变量中。 To learn more about class inheritance and polymorphism look here . 要了解有关类继承和多态的更多信息,请参见此处 I hope that this answers your question. 我希望这能回答您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM