C++ The Modern Factory

Modern Factory

Contents

My variation  that I am calling “Swappable engines factory”. It is not just engines but this makes for a short title, which I think succinctly conveys the message.

Swappable engines factory
Swappable engines factory

Here the factory method is not the second fiddle. It is instead  the key player. The product (Car in this use case) is actually assembled (put together)  in the factory function. There is no such a thing as “finished car” in this architecture.

Let us discuss the factory method.

This is a proper modern C++ .  No smart pointers needed. We are enjoying the value semantics made much easier to use with the help of a modern C++ compiler.

Copy elision on return by value is now de-facto standard in all modern compilers. C++ 17 “Guaranteed Copy Elision”, very simple explanation.

class X {} ;
//
X make_X () {
return X{}:
}

Above is in C++17 and in essence compiled into:

X x{} ;

But beware. It is far from simple as this.

This architecture will be easy to extend to offer the flexibility of composing the car not before customer (aka client) code requests a particular combination. For example.

// future extension 
// customer can choose engine, wheels and color 
// real car is returned, not the smart
// pointer to the interface

Automobile
assembly_line (
engine_tag , wheels_tag , color_tag
) ;

“Swappable Engine Factory” architecture users. do not have to worry about how many different models there are and will be. Implementation of it, simply builds the car on the spot. Usage is rather simple.

There are few more important details that are making this design possible to implement into simple and resilient modern C++.   On the next page is the GitHub gist.