Sequential coupling

Sequential coupling is where you create a class that has methods that must be called in a particular order. Method names that start with init, begin, or start may be indicative of this behavior; this may be indicative of an anti-pattern depending on the context. Sometimes, engineers use cars to explain abstract concepts, here I'll do the same.

For example, take the following class:

<?php 
 
class BadCar 
{ 
  private $started = false; 
  private $speed = 0; 
 
  private $topSpeed = 125; 
 
  /** 
   * Starts car. 
   * @return bool 
   */ 
  public function startCar(): bool 
  { 
    $this->started = true; 
 
    return $this->started; 
  } 
 
  /** 
   * Changes speed, increments by 1 if $accelerate is true, else decrease by 1. 
   * @param $accelerate 
   * @return bool 
   * @throws Exception 
   */ 
  public function changeSpeed(bool $accelerate): bool 
  { 
    if ($this->started !== true) { 
      throw new Exception('Car not started.'); 
    } 
 
    if ($accelerate == true) { 
      if ($this->speed > $this->topSpeed) { 
        return false; 
      } else { 
        $this->speed++; 
        return true; 
      } 
    } else { 
      if ($this->speed <= 0) { 
        return false; 
      } else { 
        $this->speed--; 
        return true; 
      } 
    } 
  } 
 
  /** 
   * Stops car. 
   * @return bool 
   * @throws Exception 
   */ 
  public function stopCar(): bool 
  { 
    if ($this->started !== true) { 
      throw new Exception('Car not started.'); 
    } 
 
    $this->started = false; 
 
    return true; 
  } 
} 

As you may note, we have to run the startCar function before we can use any of the other functions, or an exception is thrown. Really, if you try to accelerate a car that is not started, it shouldn't do anything, but for the sake of argument I've changed it so that the car will simply start first. In the next example of stopping the car, I have changed the class so that the method will return false if you try to stop the car without it running first:

<?php 
class GoodCar 
{ 
  private $started = false; 
  private $speed = 0; 
 
  private $topSpeed = 125; 
 
  /** 
   * Starts car. 
   * @return bool 
   */ 
  public function startCar(): bool 
  { 
    $this->started = true; 
 
    return $this->started; 
  } 
 
  /** 
   * Changes speed, increments by 1 if $accelerate is true, else decrease by 1. 
   * @param bool $accelerate 
   * @return bool 
   */ 
  public function changeSpeed(bool $accelerate): bool 
  { 
    if ($this->started !== true) { 
      $this->startCar(); 
    } 
 
    if ($accelerate == true) { 
      if ($this->speed > $this->topSpeed) { 
        return false; 
      } else { 
        $this->speed++; 
        return true; 
      } 
    } else { 
      if ($this->speed <= 0) { 
        return false; 
      } else { 
        $this->speed--; 
        return true; 
      } 
    } 
  } 
 
  /** 
   * Stops car. 
   * @return bool 
   */ 
  public function stopCar(): bool 
  { 
    if ($this->started !== true) { 
      return false; 
    } 
 
    $this->started = false; 
 
    return true; 
  } 
} 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.198.59