Saturday, March 30, 2019

How to implement the factory method pattern in C++ correctly




There's this one thing in C++ which has been making me feel uncomfortable for quite a long time, because I honestly don't know how to do it, even though it sounds simple:





Goal: to make it possible to allow the client to instantiate some object using factory methods instead of the object's constructors, without unacceptable consequences and a performance hit.



By "Factory method pattern", I mean both static factory methods inside an object or methods defined in another class, or global functions. Just generally "the concept of redirecting the normal way of instantiation of class X to anywhere else than the constructor".



Let me skim through some possible answers which I have thought of.







0) Don't make factories, make constructors.



This sounds nice (and indeed often the best solution), but is not a general remedy. First of all, there are cases when object construction is a task complex enough to justify its extraction to another class. But even putting that fact aside, even for simple objects using just constructors often won't do.



The simplest example I know is a 2-D Vector class. So simple, yet tricky. I want to be able to construct it both from both Cartesian and polar coordinates. Obviously, I cannot do:



struct Vec2 {
Vec2(float x, float y);

Vec2(float angle, float magnitude); // not a valid overload!
// ...
};


My natural way of thinking is then:



struct Vec2 {
static Vec2 fromLinear(float x, float y);
static Vec2 fromPolar(float angle, float magnitude);

// ...
};


Which, instead of constructors, leads me to usage of static factory methods... which essentially means that I'm implementing the factory pattern, in some way ("the class becomes its own factory"). This looks nice (and would suit this particular case), but fails in some cases, which I'm going to describe in point 2. Do read on.



another case: trying to overload by two opaque typedefs of some API (such as GUIDs of unrelated domains, or a GUID and a bitfield), types semantically totally different (so - in theory - valid overloads) but which actually turn out to be the same thing - like unsigned ints or void pointers.







1) The Java Way



Java has it simple, as we only have dynamic-allocated objects. Making a factory is as trivial as:



class FooFactory {
public Foo createFooInSomeWay() {
// can be a static method as well,
// if we don't need the factory to provide its own object semantics
// and just serve as a group of methods
return new Foo(some, args);

}
}


In C++, this translates to:



class FooFactory {
public:
Foo* createFooInSomeWay() {
return new Foo(some, args);

}
};


Cool? Often, indeed. But then- this forces the user to only use dynamic allocation. Static allocation is what makes C++ complex, but is also what often makes it powerful. Also, I believe that there exist some targets (keyword: embedded) which don't allow for dynamic allocation. And that doesn't imply that the users of those platforms like to write clean OOP.



Anyway, philosophy aside: In the general case, I don't want to force the users of the factory to be restrained to dynamic allocation.







2) Return-by-value



OK, so we know that 1) is cool when we want dynamic allocation. Why won't we add static allocation on top of that?



class FooFactory {
public:
Foo* createFooInSomeWay() {
return new Foo(some, args);
}
Foo createFooInSomeWay() {

return Foo(some, args);
}
};


What? We can't overload by the return type? Oh, of course we can't. So let's change the method names to reflect that. And yes, I've written the invalid code example above just to stress how much I dislike the need to change the method name, for example because we cannot implement a language-agnostic factory design properly now, since we have to change names - and every user of this code will need to remember that difference of the implementation from the specification.



class FooFactory {
public:
Foo* createDynamicFooInSomeWay() {

return new Foo(some, args);
}
Foo createFooObjectInSomeWay() {
return Foo(some, args);
}
};


OK... there we have it. It's ugly, as we need to change the method name. It's imperfect, since we need to write the same code twice. But once done, it works. Right?




Well, usually. But sometimes it does not. When creating Foo, we actually depend on the compiler to do the return value optimisation for us, because the C++ standard is benevolent enough for the compiler vendors not to specify when will the object created in-place and when will it be copied when returning a temporary object by value in C++. So if Foo is expensive to copy, this approach is risky.



And what if Foo is not copiable at all? Well, doh. (Note that in C++17 with guaranteed copy elision, not-being-copiable is no problem anymore for the code above)



Conclusion: Making a factory by returning an object is indeed a solution for some cases (such as the 2-D vector previously mentioned), but still not a general replacement for constructors.






3) Two-phase construction




Another thing that someone would probably come up with is separating the issue of object allocation and its initialisation. This usually results in code like this:



class Foo {
public:
Foo() {
// empty or almost empty
}
// ...
};


class FooFactory {
public:
void createFooInSomeWay(Foo& foo, some, args);
};

void clientCode() {
Foo staticFoo;
auto_ptr dynamicFoo = new Foo();
FooFactory factory;
factory.createFooInSomeWay(&staticFoo);

factory.createFooInSomeWay(&dynamicFoo.get());
// ...
}


One may think it works like a charm. The only price we pay for in our code...



Since I've written all of this and left this as the last, I must dislike it too. :) Why?



First of all... I sincerely dislike the concept of two-phase construction and I feel guilty when I use it. If I design my objects with the assertion that "if it exists, it is in valid state", I feel that my code is safer and less error-prone. I like it that way.




Having to drop that convention AND changing the design of my object just for the purpose of making factory of it is.. well, unwieldy.



I know that the above won't convince many people, so let's me give some more solid arguments. Using two-phase construction, you cannot:




  • initialise const or reference member variables,

  • pass arguments to base class constructors and member object constructors.




And probably there could be some more drawbacks which I can't think of right now, and I don't even feel particularly obliged to since the above bullet points convince me already.



So: not even close to a good general solution for implementing a factory.






Conclusions:



We want to have a way of object instantiation which would:





  • allow for uniform instantiation regardless of allocation,

  • give different, meaningful names to construction methods (thus not relying on by-argument overloading),

  • not introduce a significant performance hit and, preferably, a significant code bloat hit, especially at client side,

  • be general, as in: possible to be introduced for any class.



I believe I have proven that the ways I have mentioned don't fulfil those requirements.



Any hints? Please provide me with a solution, I don't want to think that this language won't allow me to properly implement such a trivial concept.



Answer




First of all, there are cases when
object construction is a task complex
enough to justify its extraction to
another class.




I believe this point is incorrect. The complexity doesn't really matter. The relevance is what does. If an object can be constructed in one step (not like in the builder pattern), the constructor is the right place to do it. If you really need another class to perform the job, then it should be a helper class that is used from the constructor anyway.




Vec2(float x, float y);
Vec2(float angle, float magnitude); // not a valid overload!


There is an easy workaround for this:



struct Cartesian {
inline Cartesian(float x, float y): x(x), y(y) {}
float x, y;
};

struct Polar {
inline Polar(float angle, float magnitude): angle(angle), magnitude(magnitude) {}
float angle, magnitude;
};
Vec2(const Cartesian &cartesian);
Vec2(const Polar &polar);


The only disadvantage is that it looks a bit verbose:




Vec2 v2(Vec2::Cartesian(3.0f, 4.0f));


But the good thing is that you can immediately see what coordinate type you're using, and at the same time you don't have to worry about copying. If you want copying, and it's expensive (as proven by profiling, of course), you may wish to use something like Qt's shared classes to avoid copying overhead.



As for the allocation type, the main reason to use the factory pattern is usually polymorphism. Constructors can't be virtual, and even if they could, it wouldn't make much sense. When using static or stack allocation, you can't create objects in a polymorphic way because the compiler needs to know the exact size. So it works only with pointers and references. And returning a reference from a factory doesn't work too, because while an object technically can be deleted by reference, it could be rather confusing and bug-prone, see Is the practice of returning a C++ reference variable, evil? for example. So pointers are the only thing that's left, and that includes smart pointers too. In other words, factories are most useful when used with dynamic allocation, so you can do things like this:



class Abstract {
public:
virtual void do() = 0;

};

class Factory {
public:
Abstract *create();
};

Factory f;
Abstract *a = f.create();
a->do();



In other cases, factories just help to solve minor problems like those with overloads you have mentioned. It would be nice if it was possible to use them in a uniform way, but it doesn't hurt much that it is probably impossible.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...