Created
July 18, 2024 07:30
-
-
Save lysenko-sergey-developer/447a7c1a6835a4920bbd1d848ed67754 to your computer and use it in GitHub Desktop.
Example of using Incapsulation + Abstraction + Interface Contract
This file contains hidden or 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 BasePerson { | |
firstName: string; | |
lastName: string; | |
password: string; | |
apiToken: string; | |
} | |
interface ShowProfile { | |
showProfile(): string; | |
} | |
class Person implements BasePerson { | |
#firstName: string; | |
#lastName: string; | |
#password: string; | |
#apiToken: string; | |
constructor( | |
firstName: string, | |
lastName: string, | |
password: string, | |
apiToken: string, | |
) { | |
this.#firstName = firstName; | |
this.#lastName = lastName; | |
this.#password = password; | |
this.#apiToken = apiToken; | |
} | |
get firstName(): string { | |
return this.#firstName; | |
} | |
get lastName(): string { | |
return this.#lastName; | |
} | |
get password(): string { | |
return this.#password; | |
} | |
get apiToken(): string { | |
return this.#apiToken; | |
} | |
} | |
class SecurePerson implements BasePerson { | |
private person: Person; | |
constructor(person: Person) { | |
this.person = person; | |
} | |
get firstName(): string { | |
return this.person.firstName; | |
} | |
get lastName(): string { | |
return this.person.lastName; | |
} | |
get password(): string { | |
return "********"; | |
} | |
get apiToken(): string { | |
return "********" + this.person.apiToken.slice(-3); | |
} | |
} | |
class PersonProfile implements ShowProfile { | |
private person: BasePerson; | |
constructor(person: BasePerson) { | |
this.person = person; | |
} | |
showProfile(): string { | |
return `First name: ${this.person.firstName} | |
Last name: ${this.person.lastName} | |
Password: ${this.person.password} | |
API Token: ${this.person.apiToken}`; | |
} | |
} | |
interface Greeter { | |
greet(person: BasePerson): string; | |
} | |
class OfficeGreeter implements Greeter { | |
greet(person: BasePerson): string { | |
return `${person.firstName} ${person.lastName} welcome to the office!`; | |
} | |
} | |
const person = new Person( | |
"John", | |
"Doe", | |
"qwerty", | |
"uvb4f-4f4f-4f4f-4001", | |
); | |
const personProfile = new PersonProfile(person); | |
console.log(personProfile.showProfile()); | |
const securePerson = new SecurePerson(person); | |
const securePersonProfile = new PersonProfile(securePerson); | |
console.log(securePersonProfile.showProfile()); | |
const officeGreeter = new OfficeGreeter(); | |
console.log(officeGreeter.greet(person)); | |
console.log(officeGreeter.greet(securePerson)); | |
officeGreeter.greet = (person: BasePerson) => { | |
// evil code | |
return `Hijack ${person.firstName} ${person.lastName} private data ${person.password} ${person.apiToken}`; | |
}; | |
console.log(officeGreeter.greet(person)); | |
console.log(officeGreeter.greet(securePerson)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment