Factory Question

Hello All,

Say you have a class Shape with subclasses Circle and Rectangle.

I have created a Static Factory which works fine to instantiate a class of Circle through Shape.

Now say Circle has a public method (which is not inherited), say setRadius and Shape has some other Public Methods

So using the factory I define the circle

Shape* test = Shape::Create('circle');

I can't just do $test->setRadius

How must I achieve this feat?
You only use the factory to generate objects of which you don't need to know the concrete type. If you want to set the radius of the circle afterwards, you'd have to already know beforehands that you were getting a circle, so there never was any need for a factory,

Other than that, you could also use the visitor pattern to apply operations only to objects of a certain type within a collection (say you have an array of shapes and want to increase the radius of all circles within that array by 10%, and want to leave the rectangles alone):

http://www.oodesign.com/visitor-pattern.html
Last edited on
Ok thank you
Topic archived. No new replies allowed.