Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While I agree with most of the other answers, in your case you don't need the classical factory pattern. As I can see, you want to have a single class which describes a shape with parametric functions for their geometry. You want to provide convenience methods to create those shapes for some special cases, like the "donut". As long as you don't want to make use of any other inheritance features like overloading some methods for special shape types, you can simply drop your other classes like <code>Donut</code> and just use the "maker" functions in your client code.</p> <p>An option to make the code still look more object-oriented (and to keep the subclasses and use their constructors in the client code), you can rewrite the maker functions to initialization functions, as I want to call them. Note that I also introduced a default constructor which we need in the specialized classes:</p> <pre><code>class ParametricShape { func x, y, z; ... public: ParametricShape(); ParametricShape(func x, func y, func z, ...); protected: void initDonutShape(float); }; class Donut : public ParametricShape { public: Donut(float radius) : ParametricShape() // call default constructor of ParametricShape { initDonutShape(radius); // initialize functions for "Donut" } }; </code></pre> <p>Now, implement the <code>initDonutShape</code> method like this:</p> <pre><code>void ParametricShape::initDonutShape(float radius) { // set the parametric functions x = ... y = ... z = ... } </code></pre> <p>rather than returning a new instance of <code>ParametricShape</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload