0Pricing
Clean Architecture & Design Patterns in Practice · บทเรียน

รูปแบบ Flyweight เพื่อประสิทธิภาพหน่วยความจำ

ค้นพบรูปแบบ Flyweight ซึ่งแบ่งปันออบเจ็กต์ที่มีรายละเอียดระดับย่อย เพื่อรองรับอินสแตนซ์จำนวนมากโดยใช้หน่วยความจำน้อยที่สุด

รูปแบบ Flyweight เพื่อประสิทธิภาพหน่วยความจำ เป็นบทเรียน Clean Architecture & Design Patterns in Practice ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 เพื่อประสิทธิภาพหน่วยความจำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รูปแบบ Adapter และ Decorator
  2. รูปแบบ Facade และ Proxy
  3. รูปแบบ Composite และ Bridge
  4. รูปแบบ Flyweight เพื่อประสิทธิภาพหน่วยความจำ
← กลับไปที่ Clean Architecture & Design Patterns in Practice