Skip to content

Instantly share code, notes, and snippets.

@jeffskelton3
Created March 29, 2023 03:30
Show Gist options
  • Save jeffskelton3/7cc0f6f9cbb29522936452b60018a04b to your computer and use it in GitHub Desktop.
Save jeffskelton3/7cc0f6f9cbb29522936452b60018a04b to your computer and use it in GitHub Desktop.
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