File tree Expand file tree Collapse file tree 2 files changed +100
-0
lines changed Expand file tree Collapse file tree 2 files changed +100
-0
lines changed Original file line number Diff line number Diff line change
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 ( ) ;
Original file line number Diff line number Diff line change
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' )
You can’t perform that action at this time.
0 commit comments