Observer模式如何减少耦合?(How does the Observer pattern reduce coupling?)
我理解Observer模式是如何工作的,但是为什么Observer模式减少了软件设计中UI和业务逻辑组件之间的耦合?
I understand how the Observer pattern works, but why is it that the Observer pattern reduces coupling between UI and business logic components in software design?
原文:https://stackoverflow.com/questions/36868384
最满意答案
CTRL + Z被解释为文件结束标记。 一旦
cin
看到该标记,它就会进入错误状态(cin.eof()
和cin.fail()
将为true
,这意味着(bool)cin
将为false
,这就是你的第一个循环停止的原因)。 处于错误状态时,cin
将不再接受任何输入。要让
cin
回到良好状态,你可以调用cin.clear()
。 一旦它恢复到良好状态,它将再次接受输入。CTRL+Z is interpreted as an end-of-file marker. Once
cin
sees that marker, it goes into an error state (cin.eof()
andcin.fail()
will betrue
, which means(bool)cin
will befalse
, that's why your first loop stops). While in an error state,cin
won't accept any more input.To get
cin
back into a good state, you can callcin.clear()
. Once it's back in a good state it will accept input once again.
相关问答
更多-
if (cin.get() != '\n'); // **Line 2** // you have sth here -^ 删除那个分号。 如果它在那里, if语句基本上什么都不做。 此外,你没有测试用户是否确实输入了一个数字...如果我输入'd'怎么办? :) while(!(cin >> x)){ // woops, something has gone wrong... // display a message to tell the user he made a mistake // ...
-
c ++ getline cin问题(c++ getline cin issue)[2021-12-11]
那么,如果你输入yes ,你似乎没有任何退出循环的方式,它只是打印test并继续其快乐的方式,试图读取更多的数据。 如果您的意思是第一次输入yes时不是打印test ,那么您需要临时更改: getline(cin,input); 至: getline (cin, input); cout << "[" << input << "]" << endl; 当你比较它时,试图找出缓冲区中的实际内容。 对于它的价值,这个代码(非常类似于你的)工作得很好: #includeint mai ... -
C ++ cin问题(C++ cin Question)[2022-02-13]
为strings定义了一个getline : std::string line; std::getline(std::cin, line); There is a getline defined for strings: std::string line; std::getline(std::cin, line); -
尝试自己实现循环退出。 即替换 int sum3 = 0, p = 0; [...] while (std::cin >> p) { sum3 += p; } 通过 int sum3 = 0, p_; string p; [...] while (std::cin >> p) { if ( p == "exit" ) break; stringstream convert(p); if ( co ...
-
使用cin检查空行(Check for empty line using cin)[2022-08-30]
使用getline ,然后处理字符串。 如果用户输入空行,则该字符串将为空。 如果没有,您可以对字符串进行进一步处理。 你甚至可以将它放在一个istringstream ,并将其视为来自cin 。 这是一个例子: std::queuedata_q; while (true) { std::string line; std::getline(std::cin, line); if (line.empty()) // line is empty, empty the ... -
与cin的isdigit问题(isdigit issue with cin)[2023-08-22]
问题是isdigit()接受一个字符,而不是一个整数。 当字符为'0' , '1'等时,它返回true ...其ascii值为48等... 请尝试这种方式: char a; while(cin>>a){ if(!isdigit(a)){ The problem is that isdigit() takes a character, not an integer. It returns true when the character is '0', '1', etc... Which has as ... -
istream::get()和istream::get(char&)的返回值不同 : int istream::get(); istream& istream::get (char& c); 所以没有参数的重载会返回read char的值,而带参数的重载会返回对同一个istream的引用,这使链接成为可能。 The return values of istream::get() and istream::get(char&) differ: int istream::get(); istream& ist ...
-
C ++ cin >>不能重新开始(C++ cin >> not starting again)[2023-02-22]
CTRL + Z被解释为文件结束标记。 一旦cin看到该标记,它就会进入错误状态( cin.eof()和cin.fail()将为true ,这意味着(bool)cin将为false ,这就是你的第一个循环停止的原因)。 处于错误状态时, cin将不再接受任何输入。 要让cin回到良好状态,你可以调用cin.clear() 。 一旦它恢复到良好状态,它将再次接受输入。 CTRL+Z is interpreted as an end-of-file marker. Once cin sees that mark ... -
cin未能获得输入(cin failing to get the input)[2022-01-05]
如果你输入cout,你会得到一个“{”,因为它是它在这里读到的最后一个字符串: cin >> input; //check to make sure it's "{" 然后你将你的数字放入intInput。 您可以在整个时间用字符串读取输入,如果它不等于“}”则将其转换为整数 #includewhile (cin >> input) { if(input == "}") break; else intInput = atoi(input. ... -
问题是>>当遇到第一个空白字符时,操作符停止读取,但为什么在这种情况下它输出完整的输入字符串,即使我们的字符串中有空格也是如此 因为你在循环中使用它。 所以每次cin吃一个你打印的单词并丢弃空白。 您在每个单词后面打印换行符这一事实意味着您不希望看到空格 - 实际上s包含空格。 测试这个的简单方法是打印: cout << s << "$"; 然而,代码最有趣的特性是while如何测试由<<返回的istream返回的值,它在布尔上下文中准确地产生你想要的:输入是否完成。 The problem is >> ...