Skip to content

Instantly share code, notes, and snippets.

@ronnelreposo
Created August 1, 2020 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronnelreposo/8e4d358ddcec7a16efcb14bcd898d15b to your computer and use it in GitHub Desktop.
Save ronnelreposo/8e4d358ddcec7a16efcb14bcd898d15b to your computer and use it in GitHub Desktop.
Object Oriented Example
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