Abstract Factory Method and Polymorphism
Posted
by
Scotty C.
on Programmers
See other posts from Programmers
or by Scotty C.
Published on 2012-11-29T16:25:49Z
Indexed on
2012/11/29
17:17 UTC
Read the original article
Hit count: 278
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?
© Programmers or respective owner