Skip to content

Commit e134ec2

Browse files
committed
设计模式-享元模式
1 parent f61d489 commit e134ec2

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.design.flyweight;
2+
3+
/**
4+
* 具体享元
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2022/1/24 14:12
8+
*/
9+
public class ConcreteFlyweight implements Flyweight {
10+
11+
private String key;
12+
13+
public ConcreteFlyweight(String key) {
14+
this.key = key;
15+
}
16+
17+
@Override
18+
public void handle(UnsharableFlyweight unsharableFlyweight) {
19+
System.out.println(this.key + "被调用,非享元信息:" + unsharableFlyweight.getMsg());
20+
}
21+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.design.flyweight;
2+
3+
/**
4+
* 抽象享元
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2022/1/24 14:12
8+
*/
9+
public interface Flyweight {
10+
11+
/**
12+
* 处理方法
13+
*
14+
* @param unsharableFlyweight
15+
* @return void
16+
* @throws
17+
* @author wliduo[i@dolyw.com]
18+
* @date 2022/1/24 14:15
19+
*/
20+
void handle(UnsharableFlyweight unsharableFlyweight);
21+
22+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.design.flyweight;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* 享元工厂
8+
*
9+
* @author wliduo[i@dolyw.com]
10+
* @date 2022/1/24 14:15
11+
*/
12+
public class FlyweightFactory {
13+
14+
private Map<String, Flyweight> flyweightMap = new HashMap<>(16);
15+
16+
/**
17+
* 工厂根据key获取Flyweight
18+
*
19+
* @param key
20+
* @return com.design.flyweight.Flyweight
21+
* @throws
22+
* @author wliduo[i@dolyw.com]
23+
* @date 2022/1/24 14:19
24+
*/
25+
public Flyweight getFlyweight(String key) {
26+
Flyweight flyweight = flyweightMap.get(key);
27+
if (flyweight == null) {
28+
flyweight = new ConcreteFlyweight(key);
29+
flyweightMap.put(key, flyweight);
30+
System.out.println(key + "不存在,进行创建");
31+
} else {
32+
System.out.println(key + "存在,成功获取");
33+
}
34+
return flyweight;
35+
}
36+
37+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.design.flyweight;
2+
3+
/**
4+
* 享元模式
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2022/1/24 13:44
8+
*/
9+
public class Main {
10+
11+
public static void main(String[] args) {
12+
// 创建工厂
13+
FlyweightFactory flyweightFactory = new FlyweightFactory();
14+
// 使用工厂创建3个apple,2个banana对象
15+
Flyweight apple1 = flyweightFactory.getFlyweight("apple");
16+
Flyweight apple2 = flyweightFactory.getFlyweight("apple");
17+
Flyweight apple3 = flyweightFactory.getFlyweight("apple");
18+
Flyweight banana1 = flyweightFactory.getFlyweight("banana");
19+
Flyweight banana2 = flyweightFactory.getFlyweight("banana");
20+
// 使用不同的对象处理发现使用的其实是同一个
21+
apple1.handle(new UnsharableFlyweight("第1次处理apple"));
22+
apple2.handle(new UnsharableFlyweight("第2次处理apple"));
23+
apple3.handle(new UnsharableFlyweight("第3次处理apple"));
24+
banana1.handle(new UnsharableFlyweight("第1次处理banana"));
25+
banana2.handle(new UnsharableFlyweight("第2次处理banana"));
26+
}
27+
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.design.flyweight;
2+
3+
/**
4+
* 非享元角色
5+
*
6+
* @author wliduo[i@dolyw.com]
7+
* @date 2022/1/24 14:14
8+
*/
9+
public class UnsharableFlyweight {
10+
11+
private String msg;
12+
13+
public UnsharableFlyweight(String msg) {
14+
this.msg = msg;
15+
}
16+
17+
public String getMsg() {
18+
return msg;
19+
}
20+
}

0 commit comments

Comments
 (0)