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. 

Link to post
Share on other sites
  • 2 months later...

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 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.

Link to post
Share on other sites
  • 2 years later...

 

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.

Link to post
Share on other sites
  • 4 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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...