Created
March 29, 2023 03:30
-
-
Save jeffskelton3/7cc0f6f9cbb29522936452b60018a04b 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
interface HttpClient<Response, Options extends {} = {}> { | |
get: (url: string, options: Options) => Promise<Response>; | |
put: (url: string, options: Options) => Promise<Response>; | |
post: (url: string, options: Options) => Promise<Response>; | |
} | |
interface CurrencyFormatter { | |
format: (str: string) => string | |
} | |
class USCurrencyFormatter implements CurrencyFormatter { | |
format = (str: string) => { | |
return "$5.00" | |
} | |
} | |
class CACurrencyFormatter implements CurrencyFormatter { | |
format = (str: string) => { | |
return "dfaksdfls" | |
} | |
} | |
interface User { | |
country: string | |
} | |
class CurrencyFormatterFactory { | |
constructor(private user: User) {} | |
getFormatter = (): CurrencyFormatter => { | |
switch (this.user.country) { | |
case "US": | |
return new USCurrencyFormatter() | |
case "CA": | |
return new CACurrencyFormatter() | |
default: | |
throw Error("You fucked up") | |
} | |
} | |
} | |
class CurrencyFormatterBuilder { | |
private selectedCurrency?: String | |
withCurrencyType(currencyType: string): CurrencyFormatterBuilder { | |
return this | |
} | |
build = (): CurrencyFormatter => { | |
switch (this.selectedCurrency) { | |
case "US": | |
return new USCurrencyFormatter(); | |
case "CA": | |
return new CACurrencyFormatter(); | |
default: | |
throw Error("You fucked up"); | |
} | |
}; | |
} | |
// business logic | |
// | |
const user = {country: "US"} | |
// const formatter = new CurrencyFormatterFactory(user).getFormatter() | |
const formatter = new CurrencyFormatterBuilder() | |
.withCurrencyType(user.country) | |
.build() | |
console.log(formatter.format("")) // $5.00 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment