Created
August 1, 2020 16:33
-
-
Save ronnelreposo/8e4d358ddcec7a16efcb14bcd898d15b to your computer and use it in GitHub Desktop.
Object Oriented Example
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 Customer { | |
firstname: string | |
middlename: string | |
lastname: string | |
age: number | |
formattedName: string | |
isLegalAge () { | |
return this.age >= 18; | |
} | |
getStatus (): string { | |
return 'Status: ' + this.isLegalAge() ? 'Allowed' : 'Not Allowed' | |
} | |
formatCustomerName (formatter: ICustomerFormatter) { | |
this.formattedName = 'Name: ' + formatter.format(this); | |
} | |
} | |
interface ICustomerFormatter { | |
format(customer: Customer): string | |
} | |
class SalesClerkCustomerFormatter implements ICustomerFormatter { | |
format(customer: Customer): string { | |
let formattedName = `${customer.lastname}, ${customer.firstname}`; | |
if (customer.middlename) { | |
if (customer.middlename[0]) { | |
return formattedName = `${formattedName} ${customer.middlename[0]}.` | |
} | |
} | |
return 'Name: ' + formattedName; | |
} | |
} | |
class ReportingCustomerFormatter implements ICustomerFormatter { | |
format(customer: Customer): string { | |
throw new Error('not implemented yet'); | |
} | |
} | |
let customer = new Customer(); | |
customer.firstname = 'sansa'; | |
customer.middlename = 'tully'; | |
customer.lastname = 'stark'; | |
customer.formatCustomerName(new SalesClerkCustomerFormatter()); | |
customer.formatCustomerName(new ReportingCustomerFormatter()); | |
customer.formattedName; // formatted customer name; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment