-
-
Save suzdalnitski/2a8742fb89f2cd5a2f5bacb3b927be7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CountryUserSelectionStrategy { | |
constructor(country) { | |
this.country = country; | |
} | |
isMatch(user) { | |
return user.country === this.country; | |
} | |
} | |
class UserSelector { | |
constructor(repository, userSelectionStrategy) { | |
this.repository = repository; | |
this.userSelectionStrategy = userSelectionStrategy; | |
} | |
selectUser() { | |
let user = null; | |
for (const u in users) { | |
if ( this.userSelectionStrategy.isMatch(u) ) { | |
user = u; | |
break; | |
} | |
} | |
return user; | |
} | |
} | |
const userRepository = new UserRepository(); | |
const userInitializer = new UserInitializer(); | |
userInitializer.initialize(userRepository); | |
const americanSelectionStrategy = new CountryUserSelectionStrategy('USA'); | |
const americanUserSelector = new UserSelector(userRepository, americanSelectionStrategy); | |
const american = americanUserSelector.selectUser(); | |
console.log('American', american); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment