fbpx
维基百科

享元模式

享元模式(英語:Flyweight Pattern)是一種軟體設計模式。它使用物件用來儘可能減少記憶體使用量;於相似物件中分享儘可能多的資訊。當大量物件近乎重複方式存在,因而使用大量記憶體時,此法適用。通常物件中的部分狀態(state)能夠共享。常見做法是把它們放在資料結構外部,當需要使用時再將它們傳遞給享元。

典型的享元模式的例子為文書處理器中以圖形結構來表示字符。一個做法是,每個字形有其字型外觀, 字模 metrics, 和其它格式資訊,但這會使每個字符就耗用上千位元組。取而代之的是,每個字符參照到一個共享字形物件,此物件會被其它有共同特質的字符所分享;只有每個字符(文件中或頁面中)的位置才需要另外儲存。

示例

Java

以下程式用來解釋上述的文字。這個例子用來解釋享元模式利用只加載执行任務时所必需的最少資料,因而減少記憶體使用量。

public enum FontEffect { BOLD, ITALIC, SUPERSCRIPT, SUBSCRIPT, STRIKETHROUGH } public final class FontData { /**  * A weak hash map will drop unused references to FontData.  * Values have to be wrapped in WeakReferences,   * because value objects in weak hash map are held by strong references.  */ private static final WeakHashMap<FontData, WeakReference<FontData>> FLY_WEIGHT_DATA = new WeakHashMap<FontData, WeakReference<FontData>>(); private final int pointSize; private final String fontFace; private final Color color; private final Set<FontEffect> effects; private FontData(int pointSize, String fontFace, Color color, EnumSet<FontEffect> effects) { this.pointSize = pointSize; this.fontFace = fontFace; this.color = color; this.effects = Collections.unmodifiableSet(effects); } public static FontData create(int pointSize, String fontFace, Color color, FontEffect... effects) { EnumSet<FontEffect> effectsSet = EnumSet.noneOf(FontEffect.class); for (FontEffect fontEffect : effects) { effectsSet.add(fontEffect); } // We are unconcerned with object creation cost, we are reducing overall memory consumption FontData data = new FontData(pointSize, fontFace, color, effectsSet); // Retrieve previously created instance with the given values if it (still) exists WeakReference<FontData> ref = FLY_WEIGHT_DATA.get(data); FontData result = (ref != null) ? ref.get() : null; // Store new font data instance if no matching instance exists if (result == null) { FLY_WEIGHT_DATA.put(data, new WeakReference<FontData> (data)); result = data; } // return the single immutable copy with the given values return result; } @Override public boolean equals(Object obj) { if (obj instanceof FontData) { if (obj == this) { return true; } FontData other = (FontData) obj; return other.pointSize == pointSize && other.fontFace.equals(fontFace) && other.color.equals(color) && other.effects.equals(effects); } return false; } @Override public int hashCode() { return (pointSize * 37 + effects.hashCode() * 13) * fontFace.hashCode(); } // Getters for the font data, but no setters. FontData is immutable. } 

外部連結

  • "使你的程式飛起來 - 實作享元以改善效能 (页面存档备份,存于互联网档案馆)"
  • ""


享元模式, 本條目存在以下問題, 請協助改善本條目或在討論頁針對議題發表看法, 此條目需要精通或熟悉相关主题的编者参与及协助编辑, 2014年10月6日, 請邀請適合的人士改善本条目, 更多的細節與詳情請參见討論頁, 此條目没有列出任何参考或来源, 2014年10月6日, 維基百科所有的內容都應該可供查證, 请协助補充可靠来源以改善这篇条目, 无法查证的內容可能會因為異議提出而移除, 此條目已列出參考文獻, 但因為沒有文內引註而使來源仍然不明, 2010年5月12日, 请加上合适的文內引註来改善这篇条目, 英語, . 本條目存在以下問題 請協助改善本條目或在討論頁針對議題發表看法 此條目需要精通或熟悉相关主题的编者参与及协助编辑 2014年10月6日 請邀請適合的人士改善本条目 更多的細節與詳情請參见討論頁 此條目没有列出任何参考或来源 2014年10月6日 維基百科所有的內容都應該可供查證 请协助補充可靠来源以改善这篇条目 无法查证的內容可能會因為異議提出而移除 此條目已列出參考文獻 但因為沒有文內引註而使來源仍然不明 2010年5月12日 请加上合适的文內引註来改善这篇条目 享元模式 英語 Flyweight Pattern 是一種軟體設計模式 它使用物件用來儘可能減少記憶體使用量 於相似物件中分享儘可能多的資訊 當大量物件近乎重複方式存在 因而使用大量記憶體時 此法適用 通常物件中的部分狀態 state 能夠共享 常見做法是把它們放在資料結構外部 當需要使用時再將它們傳遞給享元 典型的享元模式的例子為文書處理器中以圖形結構來表示字符 一個做法是 每個字形有其字型外觀 字模 metrics 和其它格式資訊 但這會使每個字符就耗用上千位元組 取而代之的是 每個字符參照到一個共享字形物件 此物件會被其它有共同特質的字符所分享 只有每個字符 文件中或頁面中 的位置才需要另外儲存 示例 编辑Java 编辑 以下程式用來解釋上述的文字 這個例子用來解釋享元模式利用只加載执行任務时所必需的最少資料 因而減少記憶體使用量 public enum FontEffect BOLD ITALIC SUPERSCRIPT SUBSCRIPT STRIKETHROUGH public final class FontData A weak hash map will drop unused references to FontData Values have to be wrapped in WeakReferences because value objects in weak hash map are held by strong references private static final WeakHashMap lt FontData WeakReference lt FontData gt gt FLY WEIGHT DATA new WeakHashMap lt FontData WeakReference lt FontData gt gt private final int pointSize private final String fontFace private final Color color private final Set lt FontEffect gt effects private FontData int pointSize String fontFace Color color EnumSet lt FontEffect gt effects this pointSize pointSize this fontFace fontFace this color color this effects Collections unmodifiableSet effects public static FontData create int pointSize String fontFace Color color FontEffect effects EnumSet lt FontEffect gt effectsSet EnumSet noneOf FontEffect class for FontEffect fontEffect effects effectsSet add fontEffect We are unconcerned with object creation cost we are reducing overall memory consumption FontData data new FontData pointSize fontFace color effectsSet Retrieve previously created instance with the given values if it still exists WeakReference lt FontData gt ref FLY WEIGHT DATA get data FontData result ref null ref get null Store new font data instance if no matching instance exists if result null FLY WEIGHT DATA put data new WeakReference lt FontData gt data result data return the single immutable copy with the given values return result Override public boolean equals Object obj if obj instanceof FontData if obj this return true FontData other FontData obj return other pointSize pointSize amp amp other fontFace equals fontFace amp amp other color equals color amp amp other effects equals effects return false Override public int hashCode return pointSize 37 effects hashCode 13 fontFace hashCode Getters for the font data but no setters FontData is immutable 外部連結 编辑UML 和 LePUS3 一種形式塑模語言 中的享元模式 使你的程式飛起來 實作享元以改善效能 页面存档备份 存于互联网档案馆 享元模式 結構模式 享元模式 取自 https zh wikipedia org w index php title 享元模式 amp oldid 64336241, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。