File tree Expand file tree Collapse file tree 5 files changed +128
-0
lines changed
DesignPattern/src/main/java/com/design/flyweight Expand file tree Collapse file tree 5 files changed +128
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments