1

Hi I'm trying to move away from procedural programming and at the same time have a better appreciation for design patterns. I would like to know what Design Pattern can best represent the code below. It's an if else statement that basically outputs a value based on the time of day. This is just a sample of several if/else if statement I have in the code. Which OOP Pattern is appropriate (Iterator, Singleton, Factory.. )?

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);
2
  • 1
    OOP does not replace if ... else. The code could certainly be improved, but how to fit it into OOP depends on how this snippet fits into the rest of your system and how reusable it should be. Commented Jun 4, 2010 at 1:39
  • The code will only be used once on a map widget which basically display a country indicator. An ajax call would be made to this code for the appropriate indicator to be displayed based on the time Commented Jun 4, 2010 at 11:53

1 Answer 1

3

I don't know if you have to apply any specific design pattern. Design patterns should only be used if you have a specific problem you are trying to solve, and the design pattern fills your need. I don't think you have such a problem here - I assume that your code works fine, in which case you can leave it like this.

Object Oriented Programming is not about applying design patterns to everything - much of your code will still be written similarly to your procedural style, but it will be oriented around your objects.

That said, I think that there is room for a bit of improvement here. Your repeat a fair bit of code here just to write hour / hours.

I would refactor that to look more like this:

$timeleft = ($timemax - $date);
$arr = array('tstatus' => 'Trading begins today at 6:00pm (less than '. $timeleft. ' hour' . ($timeleft == 1) ? '' : 's' . ' to go) -  have a great trade week!',
              'tcode'   => 'closed'                       
    );          

That will add the 's' only if it is needed, without duplicating your code too much.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that does clear up a lot, and you are right I can add the ternary conditional to the code. Really appreciate!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.