Skip to content

Commit 7d616b2

Browse files
authored
Add files via upload
1 parent 921ba98 commit 7d616b2

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/Flyweight(享元)/Poll.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//其他对象池参考:https://github.com/egret-labs/egret-game-library/blob/master/src/game/ObjectPool.ts
2+
3+
class Pool {
4+
static instance: Pool
5+
// 单例模式
6+
static getInstance() {
7+
if (!Pool.instance) {
8+
Pool.instance = new Pool()
9+
}
10+
return Pool.instance
11+
}
12+
13+
public constructor() {
14+
15+
}
16+
17+
public getPoolBySign(name) {
18+
return this[name] || (this[name] = [])
19+
}
20+
21+
public getItemByClass(name, className) {
22+
let pool = this.getPoolBySign(name)
23+
let result = (pool.length ? pool.shift() : new className())
24+
return result
25+
}
26+
27+
public recover(name, instance) {
28+
this.getPoolBySign(name).push(instance)
29+
}
30+
}
31+

0 commit comments

Comments
 (0)