Better solution then simple factory method when concrete implementations have different attributes
- by danip
abstract class Animal {
function eat() {..}
function sleep() {..}
function isSmart()
}
class Dog extends Animal {
public $blnCanBark;
function isSmart() {
return $this->blnCanBark;
}
}
class Cat extends Animal {
public $blnCanJumpHigh;
function isSmart() {
return $this->blnCanJumpHigh;
}
}
.. and so on up to 10-20 animals.
Now I created a factory using simple factory method and try to create instances like this:
class AnimalFactory {
public static function create($strName) {
switch($strName) {
case 'Dog':
return new Dog();
case 'Cat':
return new Cat();
default:
break;
}
}
}
The problem is I can't set the specific attributes like blnCanBark, blnCanJumpHigh in an efficient way.
I can send all of them as extra params to create but this will not scale to more then a few classes. Also I can't break the inheritance because a lot of the basic functionality is the same.
Is there a better pattern to solve this?