From e58abe7548f8dfb7687dca6bb64981d4b47cadb8 Mon Sep 17 00:00:00 2001 From: jskonst Date: Mon, 13 Nov 2023 08:24:04 +0300 Subject: [PATCH] oop samples --- rpgsaga/saga/src/IApplication.ts | 2 +- rpgsaga/saga/src/android.ts | 9 +++++++++ rpgsaga/saga/src/index.ts | 31 +++++++++++++++++++++++++++++++ rpgsaga/saga/src/iphone.ts | 10 ++++++++++ rpgsaga/saga/src/keep.ts | 18 ++++++++++++++++++ rpgsaga/saga/src/smartphone.ts | 30 +++++++++++++++++++++++++++--- rpgsaga/saga/src/telegram.ts | 22 ++++++++++++++++++++++ rpgsaga/saga/tsconfig.json | 1 + 8 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 rpgsaga/saga/src/android.ts create mode 100644 rpgsaga/saga/src/iphone.ts create mode 100644 rpgsaga/saga/src/keep.ts create mode 100644 rpgsaga/saga/src/telegram.ts diff --git a/rpgsaga/saga/src/IApplication.ts b/rpgsaga/saga/src/IApplication.ts index 9c6c082..ff5f881 100644 --- a/rpgsaga/saga/src/IApplication.ts +++ b/rpgsaga/saga/src/IApplication.ts @@ -3,4 +3,4 @@ export interface IApplication { install(): void; run(): void; uninstall(): void; -} \ No newline at end of file +} diff --git a/rpgsaga/saga/src/android.ts b/rpgsaga/saga/src/android.ts new file mode 100644 index 0000000..9de4782 --- /dev/null +++ b/rpgsaga/saga/src/android.ts @@ -0,0 +1,9 @@ +import { IAndroidApp } from './IAndroidApp'; +import { Smartphone } from './smartphone'; + +export class Android extends Smartphone { + installApp(app: IAndroidApp): void { + app.checkSecurity(); + super.installApp(app); + } +} diff --git a/rpgsaga/saga/src/index.ts b/rpgsaga/saga/src/index.ts index 2108b75..21dbdc8 100644 --- a/rpgsaga/saga/src/index.ts +++ b/rpgsaga/saga/src/index.ts @@ -3,8 +3,25 @@ import { CellPhone } from './cellphone'; import { LandlinePhone } from './landlinephone'; import { Babushkaphone } from './babushkaphone'; import { Eshop } from './EShop'; +import { Smartphone } from './smartphone'; +import { IAndroidApp } from './IAndroidApp'; +import { Android } from './android'; +import { Keep } from './keep'; +import { IPhone } from './iphone'; +import { Telegram } from './telegram'; + +const incYear = (phone: Phone): Phone => { + const res = new LandlinePhone(phone.phoneNumber, phone.year + 1, phone.name); + return res; +}; const first = new LandlinePhone('+7900-000 000 (123)', 1990, 'Телефон 1'); +console.log(incYear(first)); + +const a = new LandlinePhone('+7900-000 000 (123)', 1990, 'Телефон 1'); +const b = first; +console.log(b === first, a === first); + first.year = 1998; try { @@ -51,6 +68,8 @@ const phones: Array = [cp2, cp1, first]; for (const phone of phones) { console.log(phone.display()); } +phones.push(b); +phones[1].name = 'aaaaaaa'; const someReq = () => { throw new Error('Something bad happend'); @@ -70,3 +89,15 @@ shop.addStuff(cp1); shop.addStuff(cp2); shop.addStuff(first); shop.showStuff(); + +const keep = new Keep(); +const tel = new Telegram(); +const samsung1 = new Android('+79000000000', 2010, 5, 'samsung'); +samsung1.installApp(keep); +samsung1.installApp(tel); +samsung1.runApp('Keep'); +samsung1.runApp('Telegram'); + +const iphone15 = new IPhone('+79000000000', 2010, 5, 'apple'); +iphone15.installApp(tel); +iphone15.runApp('Telegram'); diff --git a/rpgsaga/saga/src/iphone.ts b/rpgsaga/saga/src/iphone.ts new file mode 100644 index 0000000..2af2fe4 --- /dev/null +++ b/rpgsaga/saga/src/iphone.ts @@ -0,0 +1,10 @@ +import { IAndroidApp } from './IAndroidApp'; +import { IIosApp } from './IIOsApp'; +import { Smartphone } from './smartphone'; + +export class IPhone extends Smartphone { + installApp(app: IIosApp): void { + app.checkSignature(); + super.installApp(app); + } +} diff --git a/rpgsaga/saga/src/keep.ts b/rpgsaga/saga/src/keep.ts new file mode 100644 index 0000000..f4be9ef --- /dev/null +++ b/rpgsaga/saga/src/keep.ts @@ -0,0 +1,18 @@ +import { IAndroidApp } from './IAndroidApp'; + +export class Keep implements IAndroidApp { + readonly name = 'Keep'; + checkSecurity(): void { + console.log('Checking security for Android'); + } + + install(): void { + console.log(`Installing ${this.name}`); + } + run(): void { + console.log(`Running ${this.name}`); + } + uninstall(): void { + console.log(`Uninstalling ${this.name}`); + } +} diff --git a/rpgsaga/saga/src/smartphone.ts b/rpgsaga/saga/src/smartphone.ts index f09da86..7d46add 100644 --- a/rpgsaga/saga/src/smartphone.ts +++ b/rpgsaga/saga/src/smartphone.ts @@ -1,11 +1,35 @@ import { IApplication } from './IApplication'; import { CellPhone } from './cellphone'; -export abstract class Smartphone extends CellPhone { - apps: IApplication[]; +export abstract class Smartphone extends CellPhone { + apps: Set; constructor(number: string, year: number, diagonal: number, public name?: string) { super(number, year, diagonal, name); - this.apps = []; + this.apps = new Set(); + } + + installApp(app: TApp) { + app.install(); + this.apps.add(app); + } + + uninstallApp(name: string) { + for (const elem of this.apps) { + if (elem.name === name) { + elem.uninstall(); + this.apps.delete(elem); + return; + } + } + } + + runApp(name: string) { + for (const elem of this.apps) { + if (elem.name === name) { + elem.run(); + return; + } + } } } diff --git a/rpgsaga/saga/src/telegram.ts b/rpgsaga/saga/src/telegram.ts new file mode 100644 index 0000000..e48ca0a --- /dev/null +++ b/rpgsaga/saga/src/telegram.ts @@ -0,0 +1,22 @@ +import { IAndroidApp } from './IAndroidApp'; +import { IIosApp } from './IIOsApp'; + +export class Telegram implements IAndroidApp, IIosApp { + checkSignature(): void { + console.log('Checking security for IPhone'); + } + readonly name = 'Telegram'; + checkSecurity(): void { + console.log('Checking security for Android'); + } + + install(): void { + console.log(`Installing ${this.name}`); + } + run(): void { + console.log(`Running ${this.name}`); + } + uninstall(): void { + console.log(`Uninstalling ${this.name}`); + } +} diff --git a/rpgsaga/saga/tsconfig.json b/rpgsaga/saga/tsconfig.json index 03a0e5a..0134630 100644 --- a/rpgsaga/saga/tsconfig.json +++ b/rpgsaga/saga/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "target": "ES6", "types": ["jest", "node"] } } \ No newline at end of file