0
View on factory design pattern implementation
Hi All I have tried implementing factory design pattern as below: https://code.sololearn.com/c7d7F6P8EWIT Could you please help me with comment on the same whether is there any improvement can be done or not in above code? Additionally, I would like to know can we change this code in some way that make it abstract factory pattern? I would like to understand this. Any view on this would be of great help. Thanks...!
12 odpowiedzi
+ 3
Nice code Ketan Lalcheta .I'll give you some tips that I consider good for optimal and nearly bug-free code.
1.Make your classes as self-contained as you possibly can(minimise dependencies on global types,functions and objects(unless they belong to the standard library)
In your case,the enum should be a public member of your class and not a global entity
+ 3
2.Avoid using plain,old,C-style enums.Opt for enum classes to avoid surprises(hard-to-find bugs) mostly brought about by implicit conversion of the enumerators to int and also to improve readability and ultimately, maintainability of your code
+ 3
4.Don't redeclare methods in derived classes as being virtual(since they are already virtual by default).Instead use the "keyword" override written just before the opening curly bracket {,since this makes it clear and explicit that a particular method overrides another declared in some base class up the hierarchy
+ 3
5.Lastly and most importantly, try as much as possible(I'm even inclined to use the words absolutely avoid) to not handle memory yourself..Instead,assign the allocated memory to a resouce handler,preferably a unique_ptr
For example.In your case you could write
unique_ptr<clsTravsl> p1{ clsTravel::GetTravelObject(Road)};
+ 2
3.Avoid defining ordinary functions within the class definition unless you really want the functions inlined by the compiler
+ 2
Finally,when calling the static getTravelObject method in main, you should fully resolve the enumerators' scope(Two scopes in this case, the enum class scope and class clsTravel scope) like this
unique_ptr<clsTravel> p1{clsTravel::GetTravelObject(clsTravel::enmTransportMode::Road)};
+ 1
This is how I would define the base class
class clsTravel
{
public:
enum class enmTransportMode{ Road , Rail , Air };
clsTravel()=default;
virtual ~clsTravel()=0;
virtual void display()=0;
static clsTravel* GetTravelObject(enmTransportMode);
};
+ 1
Next,modify the static getTravelObject method definition to return a nullptr,since the base class is now abstract and thus you cannot instantiate it
+ 1
Doing so would not cause issue to provide the same enum to argument of GetTravelObject methods from main function ?
+ 1
Wow... Something new i learnt... I never thought enum can be accessed by class name (no need to have object) directly without declaring the same as static..
0
Anthony Maina thank you so so much for this... I have tried and implemented above points in new version of code as below:
https://code.sololearn.com/cDSoZZGFf6Wp
However, I could not take enum inside of class even as public member also. I belive it might not be possible in this case as I had to create object unnecessary for abstract class which is not possible.
Please share your thoughts on this whether this code is fine or abstraction is to be removed ?
0
Yes it's possible to move the enum class into the abstract base class