Compare commits
2 Commits
dfd4dba1d7
...
475afc0db5
Author | SHA1 | Date | |
---|---|---|---|
475afc0db5 | |||
f37a8cd665 |
16
src/lab11_flyweight/ex1/Brush.java
Normal file
16
src/lab11_flyweight/ex1/Brush.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public class Brush implements DrawingTool {
|
||||||
|
private final Props props;
|
||||||
|
|
||||||
|
public Brush(Props props) {
|
||||||
|
this.props = props;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw(String text) {
|
||||||
|
System.out.println("Drawing '" + text + "' in " + props.size + ", color:" + props.color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Props(Size size, Color color) {}
|
||||||
|
}
|
17
src/lab11_flyweight/ex1/BrushFactory.java
Normal file
17
src/lab11_flyweight/ex1/BrushFactory.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class BrushFactory {
|
||||||
|
private final Map<Brush.Props, Brush> brushes = new HashMap<>();
|
||||||
|
|
||||||
|
public Brush getBrush(Brush.Props props) {
|
||||||
|
Brush brush = brushes.get(props);
|
||||||
|
if (brush == null) {
|
||||||
|
brush = new Brush(props);
|
||||||
|
brushes.put(props, brush);
|
||||||
|
}
|
||||||
|
return brush;
|
||||||
|
}
|
||||||
|
}
|
6
src/lab11_flyweight/ex1/Color.java
Normal file
6
src/lab11_flyweight/ex1/Color.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public enum Color {
|
||||||
|
BLUE,
|
||||||
|
RED
|
||||||
|
}
|
5
src/lab11_flyweight/ex1/DrawingTool.java
Normal file
5
src/lab11_flyweight/ex1/DrawingTool.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public interface DrawingTool {
|
||||||
|
void draw(String text);
|
||||||
|
}
|
31
src/lab11_flyweight/ex1/Main.java
Normal file
31
src/lab11_flyweight/ex1/Main.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
BrushFactory factory = new BrushFactory();
|
||||||
|
Brush.Props props1 = new Brush.Props(Size.THICK, Color.RED);
|
||||||
|
Brush.Props props2 = new Brush.Props(Size.THIN, Color.BLUE);
|
||||||
|
DrawingTool brush1 = factory.getBrush(props1);
|
||||||
|
DrawingTool brush2 = factory.getBrush(props1);
|
||||||
|
|
||||||
|
brush1.draw("I am drawing with my first thick red brush");
|
||||||
|
brush2.draw("I am drawing with my second thick red brush");
|
||||||
|
System.out.println("first thick red brush hashcode: " + brush1.hashCode());
|
||||||
|
System.out.println("second thick red brush hashcode: " + brush2.hashCode());
|
||||||
|
|
||||||
|
System.out.println();
|
||||||
|
DrawingTool pencil = new Pencil();
|
||||||
|
pencil.draw("Bonjour");
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
DrawingTool brush3 = factory.getBrush(props2);
|
||||||
|
DrawingTool brush4 = factory.getBrush(props2);
|
||||||
|
|
||||||
|
brush3.draw("I am drawing with my first thin blue brush");
|
||||||
|
brush4.draw("I am drawing with my second thin blue brush");
|
||||||
|
System.out.println("first thin blue brush hashcode: " + brush3.hashCode());
|
||||||
|
System.out.println("second thin blue brush hashcode: " + brush4.hashCode());
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
8
src/lab11_flyweight/ex1/Pencil.java
Normal file
8
src/lab11_flyweight/ex1/Pencil.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public class Pencil implements DrawingTool {
|
||||||
|
@Override
|
||||||
|
public void draw(String text) {
|
||||||
|
System.out.println("Pencil writes some content: " + text);
|
||||||
|
}
|
||||||
|
}
|
6
src/lab11_flyweight/ex1/Size.java
Normal file
6
src/lab11_flyweight/ex1/Size.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package lab11_flyweight.ex1;
|
||||||
|
|
||||||
|
public enum Size {
|
||||||
|
THIN,
|
||||||
|
THICK
|
||||||
|
}
|
15
src/learn/simple_flyweight/ConcreteFlyweight.java
Normal file
15
src/learn/simple_flyweight/ConcreteFlyweight.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
13
src/learn/simple_flyweight/Flyweight.java
Normal file
13
src/learn/simple_flyweight/Flyweight.java
Normal file
@ -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);
|
||||||
|
}
|
17
src/learn/simple_flyweight/FlyweightFactory.java
Normal file
17
src/learn/simple_flyweight/FlyweightFactory.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package learn.simple_flyweight;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class FlyweightFactory {
|
||||||
|
private static final Map<Integer, Flyweight> 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;
|
||||||
|
}
|
||||||
|
}
|
40
src/learn/simple_flyweight/Main.java
Normal file
40
src/learn/simple_flyweight/Main.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
src/learn/simple_flyweight/UnsharedConcreteFlyweight.java
Normal file
15
src/learn/simple_flyweight/UnsharedConcreteFlyweight.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user