typescript: Proxy Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
 * Proxy Pattern 代理是一种结构型设计模式, 让你能提供真实服务对象的替代品给客户端使用。 代理接收客户端的请求并进行一些处理 (访问控制和缓存等), 然后再将请求传递给服务对象。
 * The Subject interface declares common operations for both RealSubject and the
 * Proxy. As long as the client works with RealSubject using this interface,
 * you'll be able to pass it a proxy instead of a real subject.
 */
interface DuSubject {
    /**
     *
     * @param du
     */
    request(du:string): string; //void 也可以是方法,也可以是返回字符串等数据类型
}
 
/**
 * The RealSubject contains some core business logic. Usually, RealSubjects are
 * capable of doing some useful work which may also be very slow or sensitive -
 * e.g. correcting input data. A Proxy can solve these issues without any
 * changes to the RealSubject's code.
 */
class RealSubject implements DuSubject {
    /**
     *
     * @param du
     * @returns
     */
    public request(du:string): string { //void
        console.log('RealSubject: Handling request.');
        return "RealSubject: Handling request."+du;
    }
}
 
/**
 * The Proxy has an interface identical to the RealSubject.
 */
class DuProxy implements DuSubject {
    private realSubject: RealSubject;
 
    /**
     * The Proxy maintains a reference to an object of the RealSubject class. It
     * can be either lazy-loaded or passed to the Proxy by the client.
     */
    constructor(realSubject: RealSubject) {
        this.realSubject = realSubject;
    }
 
    /**
     * The most common applications of the Proxy pattern are lazy loading,
     * caching, controlling the access, logging, etc. A Proxy can perform one of
     * these things and then, depending on the result, pass the execution to the
     * same method in a linked RealSubject object.
     * @param du
     */
    public request(du:string):string  { //void
        let str="";
        if (this.checkAccess()) {
           str=this.realSubject.request(du);
            this.logAccess();
             
        }
        return str;
    }
 
    private checkAccess(): boolean {
        // Some real checks should go here.
        console.log('Proxy: Checking access prior to firing a real request.');
 
        return true;
    }
 
    private logAccess(): void {
        console.log('Proxy: Logging the time of request.');
    }
}
 
/**
 * The client code is supposed to work with all objects (both subjects and
 * proxies) via the Subject interface in order to support both real subjects and
 * proxies. In real life, however, clients mostly work with their real subjects
 * directly. In this case, to implement the pattern more easily, you can extend
 * your proxy from the real subject's class.
 * @param subject
 * @param du
 */
function clientCodeProxy(subject: DuSubject,du:string) {
    // ...
 
   return subject.request(du);
 
    // ...
}
 
let pubpr1="";
let pubpr2="";
let pubpr3="Geovin Du";
let pubpr4="geovindu";
 
console.log('Client: Executing the client code with a real subject:');
const realSubject = new RealSubject();
pubpr1=clientCodeProxy(realSubject,pubpr4);
 
console.log('');
 
console.log('Client: Executing the same client code with a proxy:');
const proxy = new DuProxy(realSubject);
pubpr2=clientCodeProxy(proxy,pubpr3);
 
 
let messageProxy: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du.Web';
document.body.innerHTML = messageProxy+",<br/>one="+pubpr1+",<br/>two="+pubpr2+",<br/>thee="+pubpr3+",<br/>four="+pubpr4+",<br/>TypeScript Proxy Pattern 代理模式";

  

调用:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript Hello Proxy Pattern 代理模式</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/>
    </head>
    <body>
        <script>
           // require("./dist/geovin.js");
        </script>
        <script src="dist/Proxyts.js">
 //
            //type="module"
        </script>
 
    </body>
</html>

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(17)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-10 CSharp: State Pattern in donet core 3
2022-10-10 CSharp: Memento Pattern in donet core 3
2022-10-10 CSharp: Iterator Pattern in donet core 3
2014-10-10 String Control
2013-10-10 Csharp:user WebControl Read Adobe PDF Files In Your Web Browser
2009-10-10 What Is Web 2.0
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示