From f37a8cd6658d4fd57534cc259f36d0da55560f37 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 4 Nov 2024 11:35:09 +0100 Subject: [PATCH] added flyweight example --- .../simple_flyweight/ConcreteFlyweight.java | 15 +++++++ src/learn/simple_flyweight/Flyweight.java | 13 ++++++ .../simple_flyweight/FlyweightFactory.java | 17 ++++++++ src/learn/simple_flyweight/Main.java | 40 +++++++++++++++++++ .../UnsharedConcreteFlyweight.java | 15 +++++++ 5 files changed, 100 insertions(+) create mode 100644 src/learn/simple_flyweight/ConcreteFlyweight.java create mode 100644 src/learn/simple_flyweight/Flyweight.java create mode 100644 src/learn/simple_flyweight/FlyweightFactory.java create mode 100644 src/learn/simple_flyweight/Main.java create mode 100644 src/learn/simple_flyweight/UnsharedConcreteFlyweight.java diff --git a/src/learn/simple_flyweight/ConcreteFlyweight.java b/src/learn/simple_flyweight/ConcreteFlyweight.java new file mode 100644 index 0000000..53ea7b5 --- /dev/null +++ b/src/learn/simple_flyweight/ConcreteFlyweight.java @@ -0,0 +1,15 @@ +package learn.simple_flyweight; + +public class ConcreteFlyweight extends Flyweight { + private long intrinsicState; + + public ConcreteFlyweight(long intrinsicState) { + super(); + this.intrinsicState = intrinsicState; + } + + @Override + public void operation(long extrinsicState) { + System.out.println("Flyweight " + intrinsicState + " calling operation with extrinsic state " + extrinsicState); + } +} diff --git a/src/learn/simple_flyweight/Flyweight.java b/src/learn/simple_flyweight/Flyweight.java new file mode 100644 index 0000000..b1b39b1 --- /dev/null +++ b/src/learn/simple_flyweight/Flyweight.java @@ -0,0 +1,13 @@ +package learn.simple_flyweight; + +import java.util.Arrays; + +public abstract class Flyweight { + private long[] internalStorage = new long[1000000]; + + public Flyweight() { + Arrays.fill(internalStorage, 1); + } + + public abstract void operation(long extrinsicState); +} diff --git a/src/learn/simple_flyweight/FlyweightFactory.java b/src/learn/simple_flyweight/FlyweightFactory.java new file mode 100644 index 0000000..4cb8237 --- /dev/null +++ b/src/learn/simple_flyweight/FlyweightFactory.java @@ -0,0 +1,17 @@ +package learn.simple_flyweight; + +import java.util.HashMap; +import java.util.Map; + +public class FlyweightFactory { + private static final Map flyweights = new HashMap<>(); + + public Flyweight getFlyweight(Integer key) { + Flyweight flyweight = flyweights.get(key); + if (flyweight == null) { + flyweight = new ConcreteFlyweight(key); + flyweights.put(key, flyweight); + } + return flyweight; + } +} diff --git a/src/learn/simple_flyweight/Main.java b/src/learn/simple_flyweight/Main.java new file mode 100644 index 0000000..4e439a8 --- /dev/null +++ b/src/learn/simple_flyweight/Main.java @@ -0,0 +1,40 @@ +package learn.simple_flyweight; + +import java.util.Random; + +public class Main { + private static final Random rand = new Random(); + + public static void main(String[] args) { + int rounds = 100; + + debugMemory(); + testFlyweight(rounds); + debugMemory(); + testUnsharedFlyweight(rounds); + debugMemory(); + } + + private static void debugMemory() { + long totalMemory = Runtime.getRuntime().totalMemory(); + long freeMemory = Runtime.getRuntime().freeMemory(); + long usedMemory = totalMemory - freeMemory; + System.out.printf("Memory: %d%n", usedMemory); + } + + private static void testFlyweight(int rounds) { + FlyweightFactory factory = new FlyweightFactory(); + + for (int i = 0; i < rounds; i++) { + Flyweight flyweight = factory.getFlyweight(rand.nextInt(10)); + flyweight.operation(i); + } + } + + private static void testUnsharedFlyweight(int rounds) { + for (int i = 0; i < rounds; i++) { + Flyweight flyweight = new UnsharedConcreteFlyweight(rand.nextInt(10)); + flyweight.operation(i); + } + } +} diff --git a/src/learn/simple_flyweight/UnsharedConcreteFlyweight.java b/src/learn/simple_flyweight/UnsharedConcreteFlyweight.java new file mode 100644 index 0000000..f009d01 --- /dev/null +++ b/src/learn/simple_flyweight/UnsharedConcreteFlyweight.java @@ -0,0 +1,15 @@ +package learn.simple_flyweight; + +public class UnsharedConcreteFlyweight extends Flyweight { + private long allState; + + public UnsharedConcreteFlyweight(long allState) { + super(); + this.allState = allState; + } + + @Override + public void operation(long extrinsicState) { + System.out.println("Unshared Flyweight " + allState + " calling operation with extrinsic state " + extrinsicState); + } +}