Skip to content

Commit cd62273

Browse files
committed
享元模式
1 parent 1872c53 commit cd62273

File tree

6 files changed

+175
-1
lines changed

6 files changed

+175
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* 观察者模式
2929
* 责任链模式
3030
* 原型模式
31-
31+
* 享元模式
3232

3333

3434

@@ -39,4 +39,7 @@
3939
## 一些模式的截图
4040
![Markdown](http://p1.bqimg.com/1949/f20c478905397ed2.png)
4141
![Markdown](http://p1.bqimg.com/1949/0121ae17beee2397.png)
42+
43+
44+
4245
交流群:38240957
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package structure.flyweight;/**
2+
* Created by MirsFang on 2017/3/9.
3+
*/
4+
5+
import structure.flyweight.flyweightAbs.Flyweight;
6+
7+
import java.util.Random;
8+
9+
/***
10+
*作者:MirsFang
11+
*模式:享元模式
12+
*时间:2017/03/09/下午12:35
13+
*备注 具体享元对象
14+
***/
15+
16+
public class ConcreateFlyweight_1 extends Flyweight{
17+
18+
//接受外部状态
19+
public ConcreateFlyweight_1(String extrinsic) {
20+
super(extrinsic);
21+
}
22+
23+
//根据外部状态进行逻辑处理
24+
@Override
25+
public void operate() {
26+
System.out.println(Extrinsic+"订单,订单编号"+new Random().nextInt(99999));
27+
}
28+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package structure.flyweight;/**
2+
* Created by MirsFang on 2017/3/9.
3+
*/
4+
5+
import structure.flyweight.flyweightAbs.Flyweight;
6+
7+
import java.util.HashMap;
8+
9+
/***
10+
*作者:MirsFang
11+
*模式:享元模式
12+
*时间:2017/03/09/下午12:38
13+
*备注 享元工厂
14+
***/
15+
16+
public class FlyweightFactory {
17+
//定义一个池容器
18+
private static HashMap<String,Flyweight> pool=new HashMap<>();
19+
20+
public static Flyweight getFlyweight(String Extrinsic){
21+
//需要返回的对象
22+
Flyweight flyweight=null;
23+
if(pool.containsKey(Extrinsic)){
24+
flyweight=pool.get(Extrinsic);
25+
}else {
26+
flyweight=new ConcreateFlyweight_1(Extrinsic);
27+
pool.put(Extrinsic,flyweight);
28+
}
29+
return flyweight;
30+
}
31+
32+
33+
//获取池的大小
34+
public static int getPoolSize(){
35+
return pool.size();
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package structure.flyweight;/**
2+
* Created by MirsFang on 2017/3/9.
3+
*/
4+
5+
import structure.flyweight.flyweightAbs.Flyweight;
6+
7+
/***
8+
*作者:MirsFang
9+
*模式:享元模式
10+
*时间:2017/03/09/下午12:44
11+
*备注 享元模式执行类
12+
***/
13+
14+
public class FlyweightMain {
15+
16+
17+
public static void main(String[] args) {
18+
Flyweight fly1;
19+
Flyweight fly2;
20+
Flyweight fly3;
21+
Flyweight fly4;
22+
Flyweight fly5;
23+
Flyweight fly6;
24+
25+
//根据类型创建订单对象
26+
fly1 = FlyweightFactory.getFlyweight("图书");
27+
fly2 = FlyweightFactory.getFlyweight("图书");
28+
fly3 = FlyweightFactory.getFlyweight("图书");
29+
fly4 = FlyweightFactory.getFlyweight("图书");
30+
fly5 = FlyweightFactory.getFlyweight("你懂得");
31+
fly6 = FlyweightFactory.getFlyweight("女神娃娃");
32+
33+
//调用
34+
fly1.operate();
35+
fly2.operate();
36+
fly3.operate();
37+
fly4.operate();
38+
fly5.operate();
39+
fly6.operate();
40+
41+
//查看池中对象的数量
42+
System.out.print("pool 中对象的大小 = "+FlyweightFactory.getPoolSize());
43+
44+
}
45+
46+
47+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 享元模式(Flyweight Pattern)
2+
3+
享元模式是池技术的实现方式。
4+
5+
> 使共享对象可有效地支持大量细粒度的对象
6+
7+
说白了就是通过共享对象去支持大量的不同对象
8+
9+
我们都知道new一个对象是需要成本的,CPU成本和内存成本,当你需要new出大量的对象的时候(尤其是在服务端,比如说商品抢购,瞬间订单量指数增加),特别容易造成内存溢出导致宕机。
10+
11+
这里需要用两个概念
12+
13+
* 内部状态
14+
* 对象可共享出来的信息,存储在对象内部不会随环境改变而改变
15+
* 外部状态
16+
* 外部状态是对象得以依赖的一个标记,是随环境改变而改变,不可共享的状态
17+
18+
19+
20+
| 类名 | 描述 |
21+
| -------------------- | ------- |
22+
| Flyweight | 抽象享元对象 |
23+
| ConcreateFlyweight_1 | 具体享元对象 |
24+
| FlyweightFactory | 享元工厂 |
25+
| FlyweightMain | 享元模式执行类 |
26+
27+
28+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package structure.flyweight.flyweightAbs;/**
2+
* Created by MirsFang on 2017/3/9.
3+
*/
4+
5+
/***
6+
*作者:MirsFang
7+
*模式:享元模式
8+
*时间:2017/03/09/下午12:32
9+
*备注 抽象享元对象
10+
***/
11+
12+
public abstract class Flyweight {
13+
//内部状态
14+
private String intrinsic;
15+
//外部状态
16+
protected final String Extrinsic;
17+
//要求享元角色必须接受外部状态
18+
public Flyweight(String extrinsic) {
19+
Extrinsic = extrinsic;
20+
}
21+
//定义业务操作
22+
public abstract void operate();
23+
//内部状态的get和set
24+
public String getIntrinsic() {
25+
return intrinsic;
26+
}
27+
28+
public void setIntrinsic(String intrinsic) {
29+
this.intrinsic = intrinsic;
30+
}
31+
}

0 commit comments

Comments
 (0)