Jump to content
Larry Ullman's Book Forums

Recommended Posts

I'm having trouble understanding/linking the code example to the diagram and definition for the Factory Pattern.
 

21bsq35.png

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. 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

 

I'm having trouble understanding/linking the code example to the diagram and definition for the Factory Pattern.
 

21bsq35.png

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.

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...