Skip to content

Commit 76bd5a5

Browse files
committed
bridge and facade patterns
1 parent 6010bc6 commit 76bd5a5

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

structure_patterns/Bridge.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
interface IProvider {
2+
sendMessage(messsage: string): void;
3+
connect<T>(config: T): void;
4+
disconnect(): void;
5+
}
6+
7+
class TelegramProvider implements IProvider {
8+
connect<T>(config: T): void {
9+
console.log(config)
10+
}
11+
disconnect(): void {
12+
console.log(`Disconnected TG`)
13+
}
14+
sendMessage(messsage: string): void {
15+
console.log(messsage)
16+
}
17+
}
18+
19+
class WhatAppPRovider implements IProvider {
20+
connect<T>(config: T): void {
21+
console.log(config)
22+
}
23+
disconnect(): void {
24+
console.log(`Disconnected WA`)
25+
}
26+
sendMessage(messsage: string): void {
27+
console.log(messsage)
28+
}
29+
}
30+
31+
class NotificationSender {
32+
constructor (private provider: IProvider) {}
33+
34+
send() {
35+
this.provider.connect('connect')
36+
this.provider.sendMessage('message')
37+
this.provider.disconnect()
38+
}
39+
}
40+
41+
class DelayNotificationSender extends NotificationSender {
42+
constructor(provider: IProvider) {
43+
super(provider)
44+
}
45+
46+
sendDeployed() {}
47+
}
48+
49+
const sender = new NotificationSender(new TelegramProvider())
50+
sender.send();
51+
52+
const sender2 = new NotificationSender(new WhatAppPRovider())
53+
sender2.send();

structure_patterns/Facade.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Notify {
2+
send(template: string, to: string) {
3+
console.log(`Отправляю ${template}: ${to}`)
4+
}
5+
}
6+
7+
class Log {
8+
log(message: string) {
9+
console.log(message)
10+
}
11+
}
12+
13+
class Template {
14+
private templates = [
15+
{name: 'other', template: `<h1>Шаблон<h1>`}
16+
]
17+
18+
getByName(name: string) {
19+
return this.templates.find(t => t.name === name)
20+
}
21+
}
22+
23+
class NotificationFacade {
24+
private notify: Notify;
25+
private log: Log;
26+
private template: Template;
27+
28+
constructor() {
29+
this.notify = new Notify()
30+
this.log = new Log()
31+
this.template = new Template()
32+
}
33+
34+
send(to: string, templateName: string) {
35+
const data = this.template.getByName(templateName);
36+
if (!data) {
37+
this.log.log('Не найден шаблон')
38+
return;
39+
}
40+
41+
this.notify.send(data.template, to)
42+
this.log.log('Шаблон отправлен')
43+
}
44+
}
45+
46+
const s = new NotificationFacade()
47+
s.send('a@a.ua', 'other')

0 commit comments

Comments
 (0)