From 1f3f189adfaf49d155df2223ffbd8320573dcb01 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Fri, 4 Oct 2024 14:15:48 +0200 Subject: [PATCH] added lab1 ex2 --- src/lab1_decorator/ex2/BorderDecorator.java | 16 ++++++++ src/lab1_decorator/ex2/Circle.java | 14 +++++++ src/lab1_decorator/ex2/ColorDecorator.java | 16 ++++++++ src/lab1_decorator/ex2/Rectangle.java | 16 ++++++++ src/lab1_decorator/ex2/Shape.java | 5 +++ src/lab1_decorator/ex2/ShapeDecorator.java | 14 +++++++ src/lab1_decorator/ex2/ShapeDemo.java | 39 ++++++++++++++++++++ src/lab1_decorator/ex2/Square.java | 14 +++++++ src/lab1_decorator/ex2/TextureDecorator.java | 16 ++++++++ 9 files changed, 150 insertions(+) create mode 100644 src/lab1_decorator/ex2/BorderDecorator.java create mode 100644 src/lab1_decorator/ex2/Circle.java create mode 100644 src/lab1_decorator/ex2/ColorDecorator.java create mode 100644 src/lab1_decorator/ex2/Rectangle.java create mode 100644 src/lab1_decorator/ex2/Shape.java create mode 100644 src/lab1_decorator/ex2/ShapeDecorator.java create mode 100644 src/lab1_decorator/ex2/ShapeDemo.java create mode 100644 src/lab1_decorator/ex2/Square.java create mode 100644 src/lab1_decorator/ex2/TextureDecorator.java diff --git a/src/lab1_decorator/ex2/BorderDecorator.java b/src/lab1_decorator/ex2/BorderDecorator.java new file mode 100644 index 0000000..7cd9667 --- /dev/null +++ b/src/lab1_decorator/ex2/BorderDecorator.java @@ -0,0 +1,16 @@ +package lab1_decorator.ex2; + +public class BorderDecorator extends ShapeDecorator { + private int thickness; + + public BorderDecorator(Shape shape, int thickness) { + super(shape); + this.thickness = thickness; + } + + @Override + public void draw() { + super.draw(); + System.out.printf(" with a %dpx border%n", thickness); + } +} diff --git a/src/lab1_decorator/ex2/Circle.java b/src/lab1_decorator/ex2/Circle.java new file mode 100644 index 0000000..480ffe0 --- /dev/null +++ b/src/lab1_decorator/ex2/Circle.java @@ -0,0 +1,14 @@ +package lab1_decorator.ex2; + +public class Circle extends Shape { + private double radius; + + public Circle(double radius) { + this.radius = radius; + } + + @Override + public void draw() { + System.out.println("Drawing a circle"); + } +} diff --git a/src/lab1_decorator/ex2/ColorDecorator.java b/src/lab1_decorator/ex2/ColorDecorator.java new file mode 100644 index 0000000..f7245ea --- /dev/null +++ b/src/lab1_decorator/ex2/ColorDecorator.java @@ -0,0 +1,16 @@ +package lab1_decorator.ex2; + +public class ColorDecorator extends ShapeDecorator { + private String color; + + public ColorDecorator(Shape shape, String color) { + super(shape); + this.color = color; + } + + @Override + public void draw() { + super.draw(); + System.out.printf(" in a %s color%n", color); + } +} diff --git a/src/lab1_decorator/ex2/Rectangle.java b/src/lab1_decorator/ex2/Rectangle.java new file mode 100644 index 0000000..70497cb --- /dev/null +++ b/src/lab1_decorator/ex2/Rectangle.java @@ -0,0 +1,16 @@ +package lab1_decorator.ex2; + +public class Rectangle extends Shape { + private double width; + private double height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + @Override + public void draw() { + System.out.println("Drawing a rectangle"); + } +} diff --git a/src/lab1_decorator/ex2/Shape.java b/src/lab1_decorator/ex2/Shape.java new file mode 100644 index 0000000..6400e71 --- /dev/null +++ b/src/lab1_decorator/ex2/Shape.java @@ -0,0 +1,5 @@ +package lab1_decorator.ex2; + +public abstract class Shape { + public abstract void draw(); +} diff --git a/src/lab1_decorator/ex2/ShapeDecorator.java b/src/lab1_decorator/ex2/ShapeDecorator.java new file mode 100644 index 0000000..42b117b --- /dev/null +++ b/src/lab1_decorator/ex2/ShapeDecorator.java @@ -0,0 +1,14 @@ +package lab1_decorator.ex2; + +public abstract class ShapeDecorator extends Shape { + private Shape shape; + + public ShapeDecorator(Shape shape) { + this.shape = shape; + } + + @Override + public void draw() { + shape.draw(); + } +} diff --git a/src/lab1_decorator/ex2/ShapeDemo.java b/src/lab1_decorator/ex2/ShapeDemo.java new file mode 100644 index 0000000..c5b7a93 --- /dev/null +++ b/src/lab1_decorator/ex2/ShapeDemo.java @@ -0,0 +1,39 @@ +package lab1_decorator.ex2; + +import java.util.ArrayList; +import java.util.List; + +public class ShapeDemo { + private static List shapes; + + public static void main(String[] args) { + shapes = new ArrayList(); + + Shape shape1 = new Circle(10); + Shape shape2 = new Square(5); + Shape shape3 = new Rectangle(9, 3); + + shapes.add(shape1); + shapes.add(shape2); + shapes.add(shape3); + + for(Shape s : shapes) { + s.draw(); + } + + shape1 = new BorderDecorator(shape1, 4); + shape1 = new ColorDecorator(shape1, "Red"); + shape1 = new TextureDecorator(shape1, "points"); + shape1 = new TextureDecorator(shape1, "lines"); + shapes.add(shape1); + shape2 = new ColorDecorator(shape2, "Green"); + shapes.add(shape2); + + shape3 = new BorderDecorator(shape3, 6); + shapes.add(shape3); + + for(Shape s : shapes) { + s.draw(); + } + } +} \ No newline at end of file diff --git a/src/lab1_decorator/ex2/Square.java b/src/lab1_decorator/ex2/Square.java new file mode 100644 index 0000000..b95d675 --- /dev/null +++ b/src/lab1_decorator/ex2/Square.java @@ -0,0 +1,14 @@ +package lab1_decorator.ex2; + +public class Square extends Shape { + private double size; + + public Square(double size) { + this.size = size; + } + + @Override + public void draw() { + System.out.println("Drawing a square"); + } +} diff --git a/src/lab1_decorator/ex2/TextureDecorator.java b/src/lab1_decorator/ex2/TextureDecorator.java new file mode 100644 index 0000000..8591610 --- /dev/null +++ b/src/lab1_decorator/ex2/TextureDecorator.java @@ -0,0 +1,16 @@ +package lab1_decorator.ex2; + +public class TextureDecorator extends ShapeDecorator { + private String texture; + + public TextureDecorator(Shape shape, String texture) { + super(shape); + this.texture = texture; + } + + @Override + public void draw() { + super.draw(); + System.out.printf(" using %s%n", this.texture); + } +}