lundi 5 janvier 2015

PHP Abstract Factory Pattern Implementation

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: http://ift.tt/1wDERKg http://ift.tt/1wdkJhj http://ift.tt/1xL2GFu


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:



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->capacity = $capacity;
$this->color = $color;
$this->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 */}
}


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.


The section in question is:



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 */}
}


Due to inheritance/abstraction I am forced to implement two unnecessary methods in an unrelated factory. createiPhone() and createComputer(). Am I doing the Abstract Factory Pattern right? Again, "code duplication is bad design!" What better way is there to go about this?


Aucun commentaire:

Enregistrer un commentaire