你好我試圖從移動過程編程路程,同時對設計模式有更好的理解。我想知道什麼設計模式可以最好地代表下面的代碼。這是一個if else語句,基本上是根據一天中的時間輸出一個值。這只是我在代碼中的幾個if/else if語句的示例。哪種OOP模式適合(迭代器,單例,工廠..)?PHP OOP編程 - 如何申請
if($dayval == "Sun" && $date >= 0 && $date < 18) {
$timemax = 18;
$timeleft = ($timemax - $date);
if($timeleft == 1) {
$arr = array('tstatus' => 'Trading begins today at 6:00pm (less than '. $timeleft. ' hour to go) - have a great trade week!',
'tcode' => 'closed');
}
else {
$arr = array('tstatus' => 'Trading begins today at 6:00pm (less than ' .$timeleft. ' hours to go) - have a great trade week!',
'tcode' => 'closed'
);
}
echo json_encode($arr);
}
else if($dayval == "Sun" && $date >= 18 && $date < 19) {
$timemax = 19;
$timeleft = ($timemax - $date);
if($timeleft == 1) {
$arr = array('tstatus' => 'Asian Market opening in less than ' .$timeleft. ' hour',
'tcode' => 'closed');
}
else {
$arr = array('tstatus' => 'Asian Market opening in less than ' .$timeleft. ' hours',
'tcode' => 'closed'
);
}
echo json_encode($arr);
OOP不會取代'if ... else'。代碼當然可以被改進,但是如何將它應用到OOP中取決於這個代碼片段如何適用於系統的其他部分,以及它應該如何重用。 – deceze 2010-06-04 01:39:20
該代碼只會在基本顯示國家指標的地圖小部件上使用一次。 ajax會調用這段代碼,以便根據時間顯示相應的指標 – neitony 2010-06-04 11:53:34