added lab1 ex2

This commit is contained in:
Louis Heredero 2024-10-04 14:15:48 +02:00
parent 206843abb1
commit 1f3f189adf
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
9 changed files with 150 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -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);
}
}

View File

@ -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");
}
}

View File

@ -0,0 +1,5 @@
package lab1_decorator.ex2;
public abstract class Shape {
public abstract void draw();
}

View File

@ -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();
}
}

View File

@ -0,0 +1,39 @@
package lab1_decorator.ex2;
import java.util.ArrayList;
import java.util.List;
public class ShapeDemo {
private static List<Shape> shapes;
public static void main(String[] args) {
shapes = new ArrayList<Shape>();
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();
}
}
}

View File

@ -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");
}
}

View File

@ -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);
}
}