Factory examples

hi theres,

please, can anyone point to an oF example that contains an abstract factory or factory method design pattern? just met the concept in another framework for formalized score control and I’m having trouble visualizing how it works … could maybe even be something close to the maze example from the Design Patterns book!

thanks,
adam

Hi, that’s just a software design strategy and it really does not depend on openFrameworks at all.
usually it involves a static function that will generate the objects of some kind. It really depends on what you want to achieve if it would be useful at all.
If you give a bit more info I could lead you to an answer.

Hi, thanks for your answer. I encountered the strategy as part of a python framework called abjad, where they are using factories to generate various different types of musical notation iteratively. I’ve read through the chapters in the Gamma book and I’m just trying to find a visual aid that would help cement the concept in my understanding. I’ll keep reading the abjad source and docs to do this obviously, I was just wondering if there might be an openframeworks project lying around that uses this strategy to generate graphics in a similar way. I’d be very interested to see an example of a geometric pattern factory, for instance.
There’s no specific reason, other than I tend to pick up new ideas quicker when I can lay them out spatially. If I’m being a bit vague here, let me know and I’ll try to be more specific.

I found these which might be useful.


As I mentioned, it is just a static method that usually belongs to a certain class, that will create an object of that same class based on the arguments you pass to it.

Fortunately you don’t need to look further because OF has some factory methods in its core.
The ofMesh class has several of these, as follows. (I just copy and pasted these from the ofMesh.h file)

static ofMesh_ plane(float width, float height, int columns=2, int rows=2, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
static ofMesh_ sphere(float radius, int res=12, ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
static ofMesh_ icosahedron(float radius);
static ofMesh_ icosphere(float radius, std::size_t iterations=2);
static ofMesh_ cylinder(float radius, float height, int radiusSegments=12, int heightSegments=6, int numCapSegments=2, bool bCapped = true,ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
static ofMesh_ cone(float radius, float height, int radiusSegments=12,int heightSegments=6, int capSegments=2,ofPrimitiveMode mode=OF_PRIMITIVE_TRIANGLE_STRIP);
static ofMesh_ box(float width, float height, float depth, int resX=2,int resY=2, int resZ=2);
static ofMesh_ axis(float size=1.0);

this allows you to easily create all these different types of meshes.
so if you want mesh that represents a cylinder, for example you can simply do:

ofMesh myCylinder = ofMesh::cylinder(100, 300);

and you’ll have a cylinder, instead of having to code it. Nice :smiley:

Notice that in the links i shared above they use the new keyword, which tends to be a bad idea because it can easily lead to memory leaks because of the need to delete those objects, which ends up being a responsibility of the coder who calls that factory functions. One good reason to have a factory function is for creating classes that can not be instantiated as regular objects but need to be pointers, and in such case instead of using a raw pointer (and using the new keyword) it is way much better to use a shared_ptr or unique_ptr. These will avoid memory leaks and manage their deletion automatically.

1 Like

Hey, thanks a lot - these are really useful!

I think I got the idea now, it’s simple enough really, just hadn’t encountered it before.

Nice tip on avoiding having to garbage collect new pointers that are created with the keyword
new - this is actually the approach that is used in the music notation library that I’m currently looking at,
it’s written in python, as I said, so using new is not so much of an issue because of the automatic garbage collection that occurs in that language.
So, shared_ptr or unique_ptr seem to be using templates to manage the deletion of the object after it’s out of scope, good to know!
Hey, thanks again for the help and all the best for now.

1 Like