企業(yè)網(wǎng)站建設(shè)官網(wǎng)搜索引擎關(guān)鍵詞排名
介紹
享元模式(Flyweight Pattern)是一種結(jié)構(gòu)性設(shè)計(jì)模式,旨在通過(guò)共享對(duì)象來(lái)有效地支持大量細(xì)粒度的對(duì)象。
1.定義
享元模式通過(guò)將對(duì)象狀態(tài)分為內(nèi)部狀態(tài)(可以共享)和外部狀態(tài)(不可共享),來(lái)減少內(nèi)存使用和提高性能。
2. 主要作用
- 降低內(nèi)存消耗
- 提高性能
- 共享相似對(duì)象
3. 解決的問(wèn)題
當(dāng)程序中存在大量相似對(duì)象時(shí),使用享元模式可以有效減少內(nèi)存占用,避免重復(fù)對(duì)象的創(chuàng)建。
4. 模式原理
包含角色:
- Flyweight: 抽象享元類,定義了享元對(duì)象的接口。
- ConcreteFlyweight: 具體享元類,實(shí)現(xiàn)了抽象享元類的接口,負(fù)責(zé)存儲(chǔ)內(nèi)部狀態(tài)。
- FlyweightFactory: 享元工廠類,用于創(chuàng)建和管理享元對(duì)象,確保共享。
UML類圖:
示例:
模擬一個(gè)圖形繪制的場(chǎng)景,其中使用享元模式共享相同的圖形對(duì)象(如圓):
// 享元接口
interface Shape {void draw(String color);
}// 具體享元類
class Circle implements Shape {private String color;public Circle(String color) {this.color = color;System.out.println("Creating Circle of color: " + color);}@Overridepublic void draw(String color) {System.out.println("Drawing Circle of color: " + color);}
}// 享元工廠類
class ShapeFactory {private static final Map<String, Shape> circleMap = new HashMap<>();public static Shape getCircle(String color) {Circle circle = (Circle) circleMap.get(color);if (circle == null) {circle = new Circle(color);circleMap.put(color, circle);}return circle;}
}// 使用
public class FlyweightPatternDemo {public static void main(String[] args) {ShapeFactory shapeFactory = new ShapeFactory();// 共享相同顏色的圓Shape circle1 = shapeFactory.getCircle("Red");circle1.draw("Red");Shape circle2 = shapeFactory.getCircle("Green");circle2.draw("Green");Shape circle3 = shapeFactory.getCircle("Red");circle3.draw("Red");System.out.println("Total Circles created: " + ShapeFactory.circleMap.size());}
}
打印輸出:
Creating Circle of color: Red
Drawing Circle of color: Red
Creating Circle of color: Green
Drawing Circle of color: Green
Drawing Circle of color: Red
Total Circles created: 2
🆗,從這個(gè)簡(jiǎn)單的示例,你會(huì)發(fā)現(xiàn)享元模式其實(shí)很簡(jiǎn)單,就是共享對(duì)象 復(fù)用對(duì)象,一般開(kāi)發(fā)中并不常用。
在安卓中 Handler
的Message
就使用了享元模式,因?yàn)樵诎沧恐?幾乎所有事件驅(qū)動(dòng)都是通過(guò)Message
來(lái)進(jìn)行的,可以說(shuō)它無(wú)處不在,這時(shí)候就可以使用享元模式以優(yōu)化內(nèi)存使用和提高性能。
下面就以 Message
為例,從源碼角度剖析其實(shí)現(xiàn)原理!
Message 的池化機(jī)制
在 Message 類中,有一個(gè)靜態(tài)的對(duì)象池,用于存放可重用的 Message 實(shí)例。
public static final Object sPoolSync = new Object();private static Message sPool;//這是一個(gè)鏈表的頭指針,指向池中可重用的 Message 對(duì)象private static int sPoolSize = 0;//記錄池中當(dāng)前的對(duì)象數(shù)量。private static final int MAX_POOL_SIZE = 50;//定義池的最大容量。private static boolean gCheckRecycle = true;//一個(gè)標(biāo)志位,用于檢查回收的消息是否有效。/*** Return a new Message instance from the global pool. Allows us to* avoid allocating new objects in many cases.*/public static Message obtain() {synchronized (sPoolSync) {if (sPool != null) {Message m = sPool;sPool = m.next;m.next = null;m.flags = 0; // clear in-use flagsPoolSize--;return m;}}return new Message();}public void recycle() {if (isInUse()) {if (gCheckRecycle) {throw new IllegalStateException("This message cannot be recycled because it "+ "is still in use.");}return;}recycleUnchecked();}/*** Recycles a Message that may be in-use.* Used internally by the MessageQueue and Looper when disposing of queued Messages.*/@UnsupportedAppUsagevoid recycleUnchecked() {// Mark the message as in use while it remains in the recycled object pool.// Clear out all other details.flags = FLAG_IN_USE;what = 0;arg1 = 0;arg2 = 0;obj = null;replyTo = null;sendingUid = UID_NONE;workSourceUid = UID_NONE;when = 0;target = null;callback = null;data = null;synchronized (sPoolSync) {if (sPoolSize < MAX_POOL_SIZE) {next = sPool;sPool = this;sPoolSize++;}}}
這里所謂的池 并不是一個(gè)集合容器 而是 Message
對(duì)象通過(guò) next
屬性構(gòu)成一個(gè)鏈表。每個(gè) Message
對(duì)象可以指向下一個(gè)可用的 Message
,實(shí)現(xiàn)了簡(jiǎn)單的對(duì)象池。
當(dāng)對(duì)象不再需要時(shí)(如在處理完消息后),調(diào)用recycle()
可以將其放回池中(通過(guò)設(shè)置 next
指向池頭 sPool
),這樣下次調(diào)用 obtain
時(shí)就能復(fù)用這些對(duì)象,而不是每次都新建。
在調(diào)用recycle()
可以發(fā)現(xiàn) 有一個(gè) isInUse()
方法判斷這個(gè)Message
是否正在使用,這個(gè)其實(shí)不用開(kāi)發(fā)者操心的,因?yàn)樗械南⒍家?jīng)過(guò)Looper
這個(gè) “傳送帶”,內(nèi)部自動(dòng)回收,翻開(kāi)Looper
的源碼文件你會(huì)發(fā)現(xiàn)在loopOnce
方法中最后會(huì)調(diào)用 msg.recycleUnchecked()
,如果你進(jìn)行了某種自定義操作,導(dǎo)致 Message
未能通過(guò)正常的 Handler
流程處理,那么你可能需要手動(dòng)調(diào)用 msg.recycle()
。
簡(jiǎn)單概括就是:一條由Message
組成的鏈表,你想用Message
時(shí),就從這個(gè)鏈表上 掐掉一個(gè)Message
來(lái)使用,這個(gè)掐掉操作就是將m.next = null
,當(dāng)你不再使用時(shí) 就將其放回到鏈表頭,操作就是 next = sPool
和sPool = this
。
5. 優(yōu)缺點(diǎn)
優(yōu)點(diǎn):
- 節(jié)省內(nèi)存空間。
- 提高性能,特別是創(chuàng)建和管理大量對(duì)象時(shí)。
缺點(diǎn):
- 外部狀態(tài)管理可能導(dǎo)致系統(tǒng)邏輯混亂。
6. 應(yīng)用場(chǎng)景
- 游戲中的角色、場(chǎng)景元素(如樹(shù)、建筑)等。
- 文本處理系統(tǒng)中的字符、字體。
- 大量相似對(duì)象需要頻繁創(chuàng)建的場(chǎng)景。
- …
7. 總結(jié)
享元模式通過(guò)共享對(duì)象來(lái)優(yōu)化內(nèi)存使用和性能,適合于需要?jiǎng)?chuàng)建大量相似對(duì)象的場(chǎng)景,但設(shè)計(jì)復(fù)雜性和狀態(tài)管理需要謹(jǐn)慎處理。