rob Posted February 17, 2014 Share Posted February 17, 2014 I'm having trouble understanding/linking the code example to the diagram and definition for the Factory Pattern. abstract class Shape { ... } class Rectangle extends Shape { ... } class Triangle extends Shape { ... } abstract class ShapeFactory { static function Create($type, array $sizes) { switch ($type) { case 'rectangle': return new Rectangle($sizes[0], $sizes[1]); break; case 'triangle': return new Triangle($sizes[0], $sizes[1], $sizes[2]); break; } } } $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']); Must be having an off day because I'm not seeing how the abstract factory class is being extended in the code example. I was unable to find any information in the book explaining the solid arrows or the term concrete, can anyone point me to the right pages, I must be going blind :/ Any help greatly appreciated. edit: ok I think I understand why there was confusion. The UML diagram used in the book is representative of the second reason/example given for using the factory pattern (where a base factory class is extended) but the code example references the first reason given for using a factory class and there is no UML diagram for this. Link to comment Share on other sites More sharing options...
Larry Posted February 22, 2014 Share Posted February 22, 2014 Sorry for the confusion. I'll revisit this in the book to see if I can't make it clearer. Link to comment Share on other sites More sharing options...
rob Posted February 24, 2014 Author Share Posted February 24, 2014 Larry, I think it was more me than the book; not helped by the fact I was reading the kindle version, which isn't always the best for layout. Link to comment Share on other sites More sharing options...
Larry Posted February 24, 2014 Share Posted February 24, 2014 Thanks, Rob. Still: worth my taking a look at to see if I can't make it more clear. Link to comment Share on other sites More sharing options...
jan2510 Posted May 23, 2014 Share Posted May 23, 2014 Hi Larry, I have a problem as well regarding the factory pattern. $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']); i got confused how the $obj was able to call the methods of the other class.. $obj->getArea(). Thanks Larry. Link to comment Share on other sites More sharing options...
Larry Posted May 31, 2014 Share Posted May 31, 2014 The ShapeFactory::Create() method creates one of two object types--a Rectangle or a Triangle. This returned value is assigned to $obj. So $obj will either be a Rectangle or a Triangle as if you had created them using the new operator. That's why you can then call the methods. Let me know if it's still unclear. Link to comment Share on other sites More sharing options...
kleeh Posted August 26, 2016 Share Posted August 26, 2016 I'm having trouble understanding/linking the code example to the diagram and definition for the Factory Pattern. abstract class Shape { ... } class Rectangle extends Shape { ... } class Triangle extends Shape { ... } abstract class ShapeFactory { static function Create($type, array $sizes) { switch ($type) { case 'rectangle': return new Rectangle($sizes[0], $sizes[1]); break; case 'triangle': return new Triangle($sizes[0], $sizes[1], $sizes[2]); break; } } } $obj = ShapeFactory::Create($_GET['shape'], $_GET['dimensions']); Must be having an off day because I'm not seeing how the abstract factory class is being extended in the code example. I was unable to find any information in the book explaining the solid arrows or the term concrete, can anyone point me to the right pages, I must be going blind :/ Any help greatly appreciated. edit: ok I think I understand why there was confusion. The UML diagram used in the book is representative of the second reason/example given for using the factory pattern (where a base factory class is extended) but the code example references the first reason given for using a factory class and there is no UML diagram for this. I think the problem, at least for me, is that on page 200 where the Rectangle class is shown, there is no "extends". It just says "class Rectangle {". The Triangle class says "class Triangle extends Shape {". So I couldn't see how Rectangle was accessing getArea() until I looked at Triangle and realized it was a book mistake. It's also a mistake in the code example for Rectangle. Link to comment Share on other sites More sharing options...
Larry Posted September 20, 2016 Share Posted September 20, 2016 Sorry for the delayed reply! I want to give you the right answer, but the printed 3rd edition of the book doesn't have this on page 200. Could you confirm which edition you're using and maybe include a script number reference? Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts