为什么在观察者模式中使用接口?(Why interface is used in observer pattern?)
我在书中读到应用层应该独立于表示层。 设计模式就是这样的问题的解决方案。 当一个对象应该能够通知其他对象而不假设这些对象是谁时,观察者模式是适用的。但是像addObserver这样的方法会将观察者添加到观察者列表中。 之后,观察可以知道要通知哪些对象。 以及如何使用这些列表? 在我读过的书中,它说可观察的只通过应用层中定义的接口访问观察者,以便保留系统的层结构。 然后界面使用该列表? 如果是这样,有什么区别?
I have read in the book that the application layer is supposed to be independent of the presentation layer. Design pattern are solution to problem like that. Observer pattern is applicable when an object should be able to notify other objects without making assumptions about who those objects are.But a method such as addObserver adds the observers to the list of observers. After that, observerable know which objects are to be notified. And how those list are used? In the book I read, it said that the observerable only access observer through the interface defined in the application layer so that the layer architecture of the system is preserved. Then the interface use that list? lf so, what is the difference?
原文:https://stackoverflow.com/questions/38673403
最满意答案
<?php $myarray =array ('black','blue','brown','yellow'); $row['color']=''; ?> <select name='whatever'> <?php if ($row['color']=='' || $row['color']==null){ echo "<option value=''>----Select----</option>"; } foreach ($myarray as $color){ if ($row['color']==$color){ echo"<option value='$color' selected>$color</option>"; } else{ echo"<option value='$color'>$color</option>"; } } ?>
<?php $myarray =array ('black','blue','brown','yellow'); $row['color']=''; ?> <select name='whatever'> <?php if ($row['color']=='' || $row['color']==null){ echo "<option value=''>----Select----</option>"; } foreach ($myarray as $color){ if ($row['color']==$color){ echo"<option value='$color' selected>$color</option>"; } else{ echo"<option value='$color'>$color</option>"; } } ?>
相关问答
更多-
试试这个 function getRadioValue() { for (index=0; index < document.form_data.ciudad.length; index++) { if (document.form_data.ciudad[index].checked) { var radioValue =form_data.ciudad[index].value; return pa ...
-
如果 - 语句不起作用(If-statement doesn't work)[2022-05-25]
if(mysqli_num_rows($res) > 0 AND mysqli_num_rows($res2) > 0 ) //Cehck if there are any match with one of the fielde in the database? If it matches, save in database 这部分代码不会“检查与数据库中某个字段的任何匹配”,而是因为您使用的是AND运算符,所以它需要两个条件都为真。 if(mysqli_num_rows($resultat) == 0 ... -
字符串之间的比较需要String#equals而不是== 。 所以这: if (playAgain == "yes"){ System.out.println("Restarting game..."); start = true; } else if (playAgain == "no") { System.out.println("Quitting game."); start = false; } 必须成为这个: if (playAgain.equals("yes") ...
-
WHERE语句SQL不起作用(WHERE statement SQL not working)[2022-09-04]
where子句 WHERE slots.license_id IN ( SELECT id FROM license WHERE id = '{$_GET['license']}' OR parent_id = '{$_GET['license']}' ) 没有意义,你已经加入了license表,所以你可以使用条件 where ( license.id = '{$_GET['license']}' or license.parent_id = '{$_GET['license']}' ... -
我相信你只需要像这样解析 switch (parseInt(painStatus)) { // As before.... } I believe you just need to parseInt like this switch (parseInt(painStatus)) { // As before.... }
-
sudo“-u”选项不起作用(sudo “-u” option not work)[2022-06-17]
当您使用sudo运行命令时,您实际上将用户更改为root 。 您需要root才能访问硬件。 来自维基百科 : sudo ...允许用户使用其他用户(通常是超级用户或root用户)的安全权限运行程序。 编辑:您应该考虑遵循此问题的解决方案 。 它建议构建一个可以在PHP中执行并使用root权限运行程序的二进制包装器。 When you run a command with sudo you are effectively changing the user to root. You need to be ro ... -
因为您正在从文件中读取行,所以每行以“\ n”结尾 尝试做这个instread。 def checkvalidword(tempword): tally = 0 file = open("words.txt") for x in file: if x.strip() == tempword: tally+=1 print("Added to the tally") 注意x.strip() Because you are reading lines from the ...
-
如果未选择任何内容,则$selectedStars undefined ,而不是'' 。 如果你改变了 if ($selectedStars != '') { 至 if ($selectedStars) 它按预期工作。 If nothing has been selected, $selectedStars is undefined, not ''. If you change if ($selectedStars != '') { to if ($selectedStars) it works a ...
-
以您自己的方式使用INSERT,您必须确保VALUES()子句的内容与表中列的数量和顺序完全匹配。 如果您没有包含任何列,即使它们具有默认值或自动编号,该命令也将失败。 由于您没有显示有关您的表的任何信息,我不知道这是否是您的问题的根源。 如果是问题,您可以使用INSERT的完整形式: INSERT INTO county (col1, col2, col3) VALUES ($val1, $val2, $val3) 在任何情况下都强烈建议使用此表单,因为它不对表进行任何假设(列的名称除外),并且防止命 ...