From 475afc0db55211040b54fa5a8288738ce8839096 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 4 Nov 2024 12:54:46 +0100 Subject: [PATCH] added lab11 ex1 --- src/lab11_flyweight/ex1/Brush.java | 16 ++++++++++++ src/lab11_flyweight/ex1/BrushFactory.java | 17 +++++++++++++ src/lab11_flyweight/ex1/Color.java | 6 +++++ src/lab11_flyweight/ex1/DrawingTool.java | 5 ++++ src/lab11_flyweight/ex1/Main.java | 31 +++++++++++++++++++++++ src/lab11_flyweight/ex1/Pencil.java | 8 ++++++ src/lab11_flyweight/ex1/Size.java | 6 +++++ 7 files changed, 89 insertions(+) create mode 100644 src/lab11_flyweight/ex1/Brush.java create mode 100644 src/lab11_flyweight/ex1/BrushFactory.java create mode 100644 src/lab11_flyweight/ex1/Color.java create mode 100644 src/lab11_flyweight/ex1/DrawingTool.java create mode 100644 src/lab11_flyweight/ex1/Main.java create mode 100644 src/lab11_flyweight/ex1/Pencil.java create mode 100644 src/lab11_flyweight/ex1/Size.java diff --git a/src/lab11_flyweight/ex1/Brush.java b/src/lab11_flyweight/ex1/Brush.java new file mode 100644 index 0000000..8e17cdb --- /dev/null +++ b/src/lab11_flyweight/ex1/Brush.java @@ -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) {} +} diff --git a/src/lab11_flyweight/ex1/BrushFactory.java b/src/lab11_flyweight/ex1/BrushFactory.java new file mode 100644 index 0000000..ebdf801 --- /dev/null +++ b/src/lab11_flyweight/ex1/BrushFactory.java @@ -0,0 +1,17 @@ +package lab11_flyweight.ex1; + +import java.util.HashMap; +import java.util.Map; + +public class BrushFactory { + private final Map 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; + } +} diff --git a/src/lab11_flyweight/ex1/Color.java b/src/lab11_flyweight/ex1/Color.java new file mode 100644 index 0000000..166382d --- /dev/null +++ b/src/lab11_flyweight/ex1/Color.java @@ -0,0 +1,6 @@ +package lab11_flyweight.ex1; + +public enum Color { + BLUE, + RED +} diff --git a/src/lab11_flyweight/ex1/DrawingTool.java b/src/lab11_flyweight/ex1/DrawingTool.java new file mode 100644 index 0000000..2c64cb6 --- /dev/null +++ b/src/lab11_flyweight/ex1/DrawingTool.java @@ -0,0 +1,5 @@ +package lab11_flyweight.ex1; + +public interface DrawingTool { + void draw(String text); +} diff --git a/src/lab11_flyweight/ex1/Main.java b/src/lab11_flyweight/ex1/Main.java new file mode 100644 index 0000000..88114b8 --- /dev/null +++ b/src/lab11_flyweight/ex1/Main.java @@ -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()); + + + } +} diff --git a/src/lab11_flyweight/ex1/Pencil.java b/src/lab11_flyweight/ex1/Pencil.java new file mode 100644 index 0000000..920be58 --- /dev/null +++ b/src/lab11_flyweight/ex1/Pencil.java @@ -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); + } +} diff --git a/src/lab11_flyweight/ex1/Size.java b/src/lab11_flyweight/ex1/Size.java new file mode 100644 index 0000000..bfe1a43 --- /dev/null +++ b/src/lab11_flyweight/ex1/Size.java @@ -0,0 +1,6 @@ +package lab11_flyweight.ex1; + +public enum Size { + THIN, + THICK +}