Abstract Factory Method and Polymorphism
- by Scotty C.
Being a PHP programmer for the last couple of years, I'm just starting to get into advanced programming styles and using polymorphic patterns. I was watching a video on polymorphism the other day, and the guy giving the lecture said that if at all possible, you should get rid of if statements in your code, and that a switch is almost always a sign that polymorphism is needed. At this point I was quite inspired and immediately went off to try out these new concepts, so I decided to make a small caching module using a factory method. Of course the very first thing I have to do is create a switch to decide what file encoding to choose. DANG!
class Main {
public static function methodA($parameter='')
{
switch ($parameter)
{
case 'a':
$object = new \name\space\object1();
break;
case 'b':
$object = new \name\space\object2();
break;
case 'c':
$object = new \name\space\object3();
break;
default:
$object = new \name\space\object1();
}
return (sekretInterface $object);
}
}
At this point I'm not really sure what to do. As far as I can tell, I either have to use a different pattern and have separate methods for each object instance, or accept that a switch is necessary to "switch" between them. What do you guys think?