0Pricing
Clean Architecture & Design Patterns in Practice · Ders

Bellek Verimliliği için Flyweight Kalıbı

En az bellek kullanımıyla çok sayıda örneği desteklemek için ayrıntılı nesneleri paylaşan Flyweight kalıbını keşfedin.

Bellek Verimliliği için Flyweight Kalıbı, CoddyKit'te ücretsiz bir Clean Architecture & Design Patterns in Practice dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Clean Architecture & Design Patterns in Practice öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Clean Architecture & Design Patterns in Practice kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

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.

Sıkça Sorulan Sorular

“Bellek Verimliliği için Flyweight Kalıbı” dersi ücretsiz mi?

Evet — “Bellek Verimliliği için Flyweight Kalıbı” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Clean Architecture & Design Patterns in Practice kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Clean Architecture & Design Patterns in Practice kursu toplamda 4 dersten oluşur.

“Bellek Verimliliği için Flyweight Kalıbı” dersinde ne öğreneceğim?

En az bellek kullanımıyla çok sayıda örneği desteklemek için ayrıntılı nesneleri paylaşan Flyweight kalıbını keşfedin. Clean Architecture & Design Patterns in Practice ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

Clean Architecture & Design Patterns in Practice öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te Clean Architecture & Design Patterns in Practice, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Bellek Verimliliği için Flyweight Kalıbı” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu Clean Architecture & Design Patterns in Practice dersinde kod yazıp çalıştırabilir miyim?

Evet. Her Clean Architecture & Design Patterns in Practice dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Uyarlayıcı ve Dekoratör Kalıpları
  2. Cephe ve Vekil Kalıpları
  3. Bileşik ve Köprü Kalıpları
  4. Bellek Verimliliği için Flyweight Kalıbı
← Clean Architecture & Design Patterns in Practice Sayfasına Dön