[Cs3] CS3 Question About Factory Method

Mikhail Nesterenko mikhail at cs.kent.edu
Sun Feb 16 19:51:24 EST 2014


> I was following your pizza example when I tried to make a
> FigureFactory class that has one virtual method makeFigure(int size,
> Fill* fill) that returns a new figure. I then made a SquareFactory
> and a TriangleFactory class that inherit from FigureFactory. The
> idea I had was that the methods inside these subclasses would
> override the makeFigure() method in the FigureFactory class and
> return pointers to Square or Triangle objects. However, because
> Figure is an abstract class, FigureFactory cannot return pointers to
> it. I also tried making makeFigure a purely virtual function but
> this makes FigureFactory and its subclasses abstract as well.
> 

Consider creating a factory that has a factory method which returns a
pointer to Figure class. This method should accept the figure type
(square or triangle), figure size and the object of class Fill (or the
type of fill that the figure requires).

Then, this method should allocate the square and the triangle and
return the pointer to the allocated object. Since both triangles and
squares are derived from Figure, the return type matches.

Also, consider using "enum" type to distinguish squares from triangles.

Thanks,
--
Mikhail


More information about the cs3 mailing list