mardi 11 mai 2021

Question about If/else/switch in Clean Architecture

I was reading Uncle Bob's blog, especially his article on if-else-switch. I wanted to go deeper to know if I understood what he means.

Let's imagine that I have a web app with Users, I want to display User's profil depending his sex. Some would simply do :

if(user.sex == 'female') return renderFemaleProfil(user)
if(user.sex == 'male') return renderMaleProfil(user)
return renderUnknownSexProfilError()

But to respect Clean Architecture Robert C Martin recommend to do DIP : enter image description here

If I clearly understood : I need an interface User. To create a fabric that return a polymorphic object Implementing User based on User's sex (class Male and class Female) as User

Done that, how do I display my profil ? I put a renderProfil() function in both classes that return a html string ? And just do user.renderProfil() ?

And what if I just have a single part that is different in the Male and Female profils ? Do I create renderPart1(), renderPart2() and renderPart3Male() for Male class and renderPart3Female() for Female class ? And call them in a single function renderProfil() ?

And is it worth it ?

Thank to enlighten me on those points :)

Aucun commentaire:

Enregistrer un commentaire