DEV Community

loading...

Discussion on: Refactoring: My 6 favorite patterns

Collapse
ronielramos profile image
Roniel

About #1, how do you implement "getUser(userData)"?

Collapse
brycedooley profile image
Bryce Author

I'd probably use a switch statement to instantiate the appropriate user type - so something like:

function getUser(userData) {
  const userType = userData.favorites.food;

  switch(userType) {
    case 'pizza':
      return new PizzaUser(userData);
    ... 
  }
}
Collapse
chakrihacker profile image
Subramanya Chakravarthy

You might have included this snippet in the article

Collapse
toledoroy profile image
Roy Toledo

This is like the #1 #1 part of the article... You might want to add it.

Collapse
awakeel profile image
Abdul wakeel

Switch statement itself sometime bound you, we can use factory pattern to return the desire class object ;)

Thread Thread
brycedooley profile image
Bryce Author

Thanks, Abdul. Could you provide an example of what the code might look like? Most factory functions I've seen still involve a switch statement or a series of if/else statements.