Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d64c9da

Browse files
author
deeper
committed
add flyweight
1 parent fb7b25a commit d64c9da

File tree

4 files changed

+75
-0
lines changed

4 files changed

+75
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.ruoxu.pattern.flyweight;
2+
/**
3+
* 享元模式(是对象池的一种实现,flyweight轻量级)
4+
* 享元模式尽可能减少内存使用量
5+
*
6+
* 定义:使用共享对象可有效地支持大量的细粒度的对象。
7+
*
8+
* 使用场景:
9+
* 1.系统中存在大量相似对象
10+
* 2.细粒度的对象都具备较接近的外部状态,而且内部状态与环境无关,也就是说对象没有特定身份。享元模式通过消息吃的形式有效地减少了重复对象的存在。它通过内部状态标示某个种类的对象,外部程序根据这个不会变化的内部状态从消息池中取出对象,使得同一类对象可以被复用,避免大量重复对象。(这句话结合例子即可理解)
11+
* 3.需要缓冲池的场景
12+
*/
13+
public class Demo {
14+
public static void main(String[] args) {
15+
Ticket ticket_new1 = TicketFactory.getTicket("北京", "上海");
16+
ticket_new1.showTicketInfo("上铺");
17+
18+
Ticket ticket_new2 = TicketFactory.getTicket("北京", "杭州");
19+
ticket_new2.showTicketInfo("下铺");
20+
21+
Ticket ticket_cache1 = TicketFactory.getTicket("北京", "上海");
22+
ticket_cache1.showTicketInfo("下铺");
23+
24+
Ticket ticket_cache2 = TicketFactory.getTicket("北京", "杭州");
25+
ticket_cache2.showTicketInfo("下铺");
26+
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.ruoxu.pattern.flyweight;
2+
3+
public interface Ticket {
4+
void showTicketInfo(String bunk);
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.ruoxu.pattern.flyweight;
2+
3+
import java.util.Map;
4+
import java.util.concurrent.ConcurrentHashMap;
5+
6+
public class TicketFactory {
7+
private static Map<String,Ticket> sTicketMap = new ConcurrentHashMap<>();
8+
public static Ticket getTicket(String from,String to){
9+
String key = from + "-" + to;
10+
if(sTicketMap.containsKey(key)){
11+
System.out.println("使用缓存==>" + key );
12+
return sTicketMap.get(key);
13+
}else{
14+
System.out.println("创建对象==>" + key);
15+
Ticket ticket = new TrainTicket(from, to);
16+
sTicketMap.put(key, ticket);
17+
return ticket;
18+
}
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.ruoxu.pattern.flyweight;
2+
3+
import java.util.Random;
4+
5+
public class TrainTicket implements Ticket{
6+
public String from;
7+
public String to;
8+
public String bunk;// 铺位
9+
public int price;
10+
11+
public TrainTicket(String from,String to) {
12+
this.from = from;
13+
this.to = to;
14+
}
15+
16+
@Override
17+
public void showTicketInfo(String bunk) {
18+
price = new Random().nextInt(300);
19+
System.out.println("购买从 " + from + " 到 "+ to + " 的 " + bunk + " 火车票" + ",价格: " + price);
20+
}
21+
22+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /