Skip to content

Commit 1cd9804

Browse files
committed
proxy pattern
1 parent 5151be1 commit 1cd9804

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

structure_patterns/Proxy.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
interface IPaymentAPI {
2+
getPaymenyDetail(id: number): IPaymentDetail | undefined;
3+
}
4+
5+
interface IPaymentDetail {
6+
id: number;
7+
sum: number;
8+
}
9+
10+
class PaymentAPI implements IPaymentAPI {
11+
private data = [{id: 1, sum: 1000 }]
12+
getPaymenyDetail(id: number): IPaymentDetail | undefined {
13+
return this.data.find(d => d.id === id)
14+
}
15+
}
16+
17+
class PaymentAccessProxy implements IPaymentAPI {
18+
constructor(private api: PaymentAPI, private userId: number) {}
19+
20+
getPaymenyDetail(id: number): IPaymentDetail | undefined{
21+
if (this.userId === 1) {
22+
return this.api.getPaymenyDetail(id)
23+
}
24+
console.log('Попытка получить данные платежа')
25+
return undefined
26+
}
27+
}
28+
29+
const proxy = new PaymentAccessProxy(new PaymentAPI(), 1)
30+
console.log(proxy.getPaymenyDetail(1))
31+
32+
const proxy2 = new PaymentAccessProxy(new PaymentAPI(), 2)
33+
console.log(proxy.getPaymenyDetail(2))

0 commit comments

Comments
 (0)