نمط Flyweight لكفاءة الذاكرة
اكتشف نمط Flyweight الذي يشارك الكائنات الدقيقة لدعم أعداد كبيرة من المثيلات بأقل قدر من الذاكرة
نمط Flyweight لكفاءة الذاكرة درس مجاني في Clean Architecture & Design Patterns in Practice على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Clean Architecture & Design Patterns in Practice، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Memory Problem
Some applications must hold huge numbers of similar objects: characters in a document, tiles in a game, particles in a simulation.
Naively creating one full object per element can exhaust memory. The Flyweight pattern attacks this directly.
Core Idea: Share What Repeats
Flyweight splits an object state into two parts:
- Intrinsic state: shared, context-independent (e.g. a glyph shape).
- Extrinsic state: unique per use, passed in from outside (e.g. position).
Only the intrinsic part is stored once and reused.
A Concrete Scenario
Imagine rendering thousands of trees in a forest. Every tree of the same species shares the same texture and mesh.
Storing that texture once and reusing it across all instances is the essence of Flyweight.
The Flyweight Class
The flyweight holds only intrinsic, shareable data.
class TreeType {
final String name;
final String texture;
TreeType(String name, String texture) {
this.name = name;
this.texture = texture;
}
}The Flyweight Factory
A factory ensures flyweights are created once and reused, returning the existing instance when possible.
class TreeTypeFactory {
private static final Map<String, TreeType> pool = new HashMap<>();
static TreeType get(String name, String tex) {
return pool.computeIfAbsent(name, k -> new TreeType(name, tex));
}
}Passing Extrinsic State
The context object stores only the unique data and references the shared flyweight.
class Tree {
final int x, y;
final TreeType type;
Tree(int x, int y, TreeType type) {
this.x = x; this.y = y; this.type = type;
}
}Putting It Together
Thousands of Tree objects share a handful of TreeType flyweights.
public static void main(String[] args) {
java.util.List<Tree> forest = new java.util.ArrayList<>();
for (int i = 0; i < 100000; i++) {
TreeType t = TreeTypeFactory.get("Oak", "oak.png");
forest.add(new Tree(i, i, t));
}
System.out.println("Trees: " + forest.size());
}Intrinsic vs Extrinsic Discipline
The pattern only works if intrinsic state is truly immutable and shared. If you mutate a flyweight, every context using it is affected.
Always make flyweight fields final.
Relationship to Other Patterns
- The factory in Flyweight resembles an Object Pool, but pool objects are checked out exclusively while flyweights are shared concurrently.
- Flyweights are often combined with Composite for tree-like shared structures.
When to Use It
Reach for Flyweight when:
- You have a very large number of objects.
- Storage cost is high due to repeated state.
- That repeated state can be cleanly separated as intrinsic.
If object counts are modest, the indirection is not worth it.
Trade-offs
Flyweight trades CPU and complexity for memory. Passing extrinsic state on each call and looking up shared instances adds overhead. Use it only when memory is the real bottleneck.
Quick Check
Test your understanding of the Flyweight pattern.
Recap
You learned the Flyweight structural pattern.
- Split state into intrinsic (shared) and extrinsic (per-use).
- A factory reuses shared flyweights.
- It trades CPU for large memory savings when instance counts are huge.
الأسئلة الشائعة
هل درس «نمط Flyweight لكفاءة الذاكرة» مجاني؟
نعم — نص درس «نمط Flyweight لكفاءة الذاكرة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Clean Architecture & Design Patterns in Practice، انتقل إلى CoddyKit PRO. تتضمن دورة Clean Architecture & Design Patterns in Practice 4 دروس في المجموع.
ماذا ستتعلم في «نمط Flyweight لكفاءة الذاكرة»؟
اكتشف نمط Flyweight الذي يشارك الكائنات الدقيقة لدعم أعداد كبيرة من المثيلات بأقل قدر من الذاكرة تتمرن على Clean Architecture & Design Patterns in Practice مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Clean Architecture & Design Patterns in Practice؟
لا تُشترط خبرة سابقة. Clean Architecture & Design Patterns in Practice على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «نمط Flyweight لكفاءة الذاكرة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Clean Architecture & Design Patterns in Practice هذا؟
نعم. كل درس في Clean Architecture & Design Patterns in Practice يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- أنماط Adapter وDecorator
- أنماط Facade وProxy
- أنماط Composite وBridge
- نمط Flyweight لكفاءة الذاكرة