0
Please suggest on this abstract factory pattern
Hi Refer code below: Is there any modification needed ? In terms of code readability or performance or design aspect, please feel free to suggest. https://sololearn.com/compiler-playground/cLsTK4TbZNuA/?ref=app
8 Answers
+ 2
Just my personal opinion:
It looks ok, but I've almost given up on the inheritance part of OOP. You can live without it and it will make your life a lot simpler.
Inheritance and design patterns really adds a lot of complication to the code. Banana with a gorilla attached to it is an accurate description.
Probably the main advantage is for job security. Make your code so complicated that you become indispensable for maintaining it.
--end of rant.
suggestion:
for probable compiler optimization, maybe add the override specifier for the derived draw methods?
or use const for base and const override for derived methods...
or maybe just use one factory function instead of the complicated abstract factory class that you have to initialize and delete?
https://sololearn.com/compiler-playground/cgwVQmrboe9s/?ref=app
+ 2
it's good to know these patterns, and there might be actual need for them, but I feel it's better to use simpler methods if possible.
Yes, I think inheritance is responsible for a lot of unnecessary code complexity. You end up with a lot of tightly coupled blocks of code that is hard to maintain. It is overused unnecessarily.
Think of it this way: we use OOP to avoid code duplication and create an extensible architecture, but we often end up with more code and complicated hierarchy than if we just simply duplicated some simple code instead. And the code is not really extended afterwards. You end up with just a lot of extra baggage.
Like packing everything for a vacation but not really using most of it in the end.
For simple problems, perhaps we don't need to use OOP at all.
+ 1
Thanks a lot Bob_Li
Do uiy mean to say that inheritance is highly overvalued and not used mostly?
Also what other change you suggest apart from const and override?
+ 1
It's a matter of personal preference, in my opinion.
I think your suggestion is preferrable and easier to maintain.
As I said before, making complicated design in anticipation of future demand have to be based on something more concrete than just a guess. Often it is not necessary and it just becomes premature optimization.
Would you rather refactor a few simple codes or maintain and extend a complicated codebase?
+ 1
Perhaps geared to game design, but the ideas can be adapted to other use cases as alternative to deep hierarchy resulting from OOP
https://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
I'm not against OOP, but keeping your classes as simple as possible is a good idea. Bringing in inheritance and going crazy with it is asking for trouble. Using procedural or functional methods to manipulate them is usually better than complicating the classes with too much functionalities and trying to encapsulate too much data.
Like your abstract base factory. Does it need to be a class? Why not just write a factory function?
Always the question of do we need to define a class. Maybe it's just something we learned at school.
+ 1
I have always been confused about
1.
making it simpler initially and then refactoring to avoid initial complicated design (sticking to keep it simple and stupid)
2. Smart engineers predict the future and allow design to have more flexibility to avoid major changes when a small change is expected by business
I understand that design is not a step by step 1 or 0 kind of implementation and need to evolve
+ 1
It's the predict the future part that's problematic.
0
Hi Bob_Li just wanted to know more on inheritance
We have a very common question about parking lot design when we study LLD.
There also we have widely used inheritance like vehicle class and it is inheritance for 2 and 4 veehler and heavy vehicle. Do you suggest there also no inheritance?
I had initially suggested interviewer to go with single class and enum for different types. She rejected this approach saying enum to be used when we have limited states in future also (like human is dead or hospitalized or bed rested or dead, nothing more) but vehicle is more generic and can come new type at any point of time. So it was food to Have inheritance and class in vehicle design.
Just curious to know your view in this point. I appreciate your help đ