A Factory Object

Hi,

Could anyone give me an example of a Factory Object inside RhinoCommon?

I am trying to understand how factory objects behave and how they are invoked. (Preferably in Python)

Thanks in advance.

Hi @ivelin.peychev,

In your linked wikipedia examples there are already some sample codes for factory Classes in Python

In Rhinocommon the first thing that comes to mind, which in its pattern is similar to a factory would be the Curve class. It has no publicly available constructors for users of the SDK and new Curve objects are rather created inside of static functions. This is an abstraction of the internal constructor of the Curve class, since no User generated Code can call this constructor, Its code could be changed at any time (for example changing the amount or type of arguments to it) without breaking any user generated Code.
This is a way different approach to classes inside of Rhinocommon, then for example The Plane or Point3d Classes.

2 Likes

Thanks @lando.schumpich,

Yes, I saw the examples but I cannot grasp how constructors and factories correlate. This article is written in rather technical (computer science) language.

I am looking for a plain English explanation or direct example. Since I think I know how RhinoCommon implements stuff I thought in order for me to understand what factories are I need to identify one :slight_smile:

I’ll take a look at the Curve and Point3d classes and see the differences.

Thanks a lot again.

Hello,

I think this is a pretty good practical example of where you might use a factory method as oppos d to the simple __init__ constructor.

python recipes by Dave beasley

1 Like

Hi Ivelin,

Just in case this hasn’t clicked: there are different types of factory out there… e.g. the Abstract Factory pattern, the Simple Factory class and the Factory method. These are different but are all concerned with decoupling and responsibility.

Re resources, the original sourcebook on this aspect of software design is Design Patterns by the “Gang of Four”. More recently there is Head First Design Patterns which I think is a great starting point for a newcomer (provided you can live with the cuteness of the Head First books), although the examples are in Java.

Regards
Jeremy

2 Likes

Just to return to this topic I have started rewriting the Java examples from the book :open_book: in Python :snake:, including the abstract factory :factory: pattern here ! Yes the book is very good!

3 Likes

Is a class only consider a factory when it initializes and returns other classes, or also if it returns specific instances of itself through static methods? An example, could potentially be something like a polyline, which has static methods that return certain polyline “primitives”, like triangles, pentagons, hexagons, etc.

I think a key part of the factory method pattern is that the caller doesn’t know the specific class of the returned object so if your poly lines, meshes and surfaces all have a ToTriangle() method returning an object with 3 vertices which behaves like a Triangle then those are factory methods

1 Like