title: PHP Abstract Factory Pattern Implementation
body: <p>I am reading about the different patterns available out there. Currently I am on the Abstract Factory Pattern and I think I have a pretty good grasp on it. My resources are, besides wikipedia:
  <a href="http://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm" rel="nofollow">http://www.tutorialspoint.com/design_pattern/abstract_factory_pattern.htm</a>
  <a href="http://www.oodesign.com/abstract-factory-pattern.html" rel="nofollow">http://www.oodesign.com/abstract-factory-pattern.html</a>
  <a href="https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/AbstractFactory" rel="nofollow">https://github.com/domnikl/DesignPatternsPHP/tree/master/Creational/AbstractFactory</a></p>
  
  <p>I am using Apple and its products to make a sample Abstract Factory pattern as an example. I understand that code duplication is bad design, hence the reason I am writing this. My code so far is:</p>
  
  <pre><code>abstract class AbstractAppleFactory {
      abstract public function createiPod( $capacity, $type, $color, $engraving );
      abstract public function createiPhone( $capacity, $type, $color, $antenna );
      abstract public function createComputer( $type, $HDCapacity, $CPU, $ram );
  }
  
  class iPodFactory extends AbstractAppleFactory {
      public function createiPod( $capacity, $type, $color, $engraving ) {
          $class = 'iPod' . $type;
  
          return new $class( $capacity, $color, $engraving);
      }
  
      public function createiPhone( $capacity, $type, $color, $antenna ){ /* no implementation necessary */}
      public function createComputer( $type, $HDCapacity, $CPU, $ram ){ /* no implementation necessary */}
  }
  
  interface iPlayer {
      public function play();
      public function stop();
      public function fastForward();
      public function rewind();
  }
  
  abstract class iPod implements iPlayer {
      protected $capacity;
      protected $color;
      protected $engraving;
  
      public function __construct( $capacity, $color, $engraving = null ) {
          $this-&gt;capacity = $capacity;
          $this-&gt;color = $color;
          $this-&gt;engraving = $engraving;
      }
  }
  
  class iPodClassic extends iPod {
      public function play() {/* implementation goes here */}
      public function stop() {/* implementation goes here */}
      public function fastForward() {/* implementation goes here */}
      public function rewind() {/* implementation goes here */}
  }
  class iPodShuffle extends iPod {
      public function play() {/* implementation goes here */}
      public function stop() {/* implementation goes here */}
      public function fastForward() {/* implementation goes here */}
      public function rewind() {/* implementation goes here */}
  }
  </code></pre>
  
  <p>etc. etc. There is just too much code to put on here. I know that it is better to organize in directories and namespaces. That is not what I am learning right now. I am learning about patterns and OOP concepts.</p>
  
  <p>The section in question is:</p>
  
  <pre><code>class iPodFactory extends AbstractAppleFactory {
      public function createiPod( $capacity, $type, $color, $engraving ) {
          $class = 'iPod' . $type;
  
          return new $class( $capacity, $color, $engraving);
      }
  
      public function createiPhone( $capacity, $type, $color, $antenna ){ /* no implementation necessary */}
      public function createComputer( $type, $HDCapacity, $CPU, $ram ){ /* no implementation necessary */}
  }
  </code></pre>
  
  <p>Due to inheritance/abstraction I am forced to implement two unnecessary methods in an unrelated factory. <code>createiPhone()</code> and <code>createComputer()</code>. Am I doing the Abstract Factory Pattern right? Again, "code duplication is bad design!" What better way is there to go about this?</p>