DEV Community

Discussion on: Refactoring: My 6 favorite patterns

Collapse
 
brycedooley profile image
Bryce

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
 
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

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.

Collapse
 
toledoroy profile image
Roy Toledo

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