Skip to content

Commit

Permalink
oop samples
Browse files Browse the repository at this point in the history
  • Loading branch information
jskonst committed Nov 13, 2023
1 parent 4b70e3b commit e58abe7
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 4 deletions.
2 changes: 1 addition & 1 deletion rpgsaga/saga/src/IApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ export interface IApplication {
install(): void;
run(): void;
uninstall(): void;
}
}
9 changes: 9 additions & 0 deletions rpgsaga/saga/src/android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IAndroidApp } from './IAndroidApp';
import { Smartphone } from './smartphone';

export class Android extends Smartphone<IAndroidApp> {
installApp(app: IAndroidApp): void {
app.checkSecurity();
super.installApp(app);
}
}
31 changes: 31 additions & 0 deletions rpgsaga/saga/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,6 +68,8 @@ const phones: Array<Phone> = [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');
Expand All @@ -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');
10 changes: 10 additions & 0 deletions rpgsaga/saga/src/iphone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IAndroidApp } from './IAndroidApp';
import { IIosApp } from './IIOsApp';
import { Smartphone } from './smartphone';

export class IPhone extends Smartphone<IIosApp> {
installApp(app: IIosApp): void {
app.checkSignature();
super.installApp(app);
}
}
18 changes: 18 additions & 0 deletions rpgsaga/saga/src/keep.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
30 changes: 27 additions & 3 deletions rpgsaga/saga/src/smartphone.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { IApplication } from './IApplication';
import { CellPhone } from './cellphone';

export abstract class Smartphone extends CellPhone {
apps: IApplication[];
export abstract class Smartphone<TApp extends IApplication> extends CellPhone {
apps: Set<TApp>;

constructor(number: string, year: number, diagonal: number, public name?: string) {
super(number, year, diagonal, name);
this.apps = [];
this.apps = new Set<TApp>();
}

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;
}
}
}
}
22 changes: 22 additions & 0 deletions rpgsaga/saga/src/telegram.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}
1 change: 1 addition & 0 deletions rpgsaga/saga/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"target": "ES6",
"types": ["jest", "node"]
}
}

0 comments on commit e58abe7

Please sign in to comment.