added lab1 ex2
This commit is contained in:
parent
206843abb1
commit
1f3f189adf
16
src/lab1_decorator/ex2/BorderDecorator.java
Normal file
16
src/lab1_decorator/ex2/BorderDecorator.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
14
src/lab1_decorator/ex2/Circle.java
Normal file
14
src/lab1_decorator/ex2/Circle.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
16
src/lab1_decorator/ex2/ColorDecorator.java
Normal file
16
src/lab1_decorator/ex2/ColorDecorator.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
16
src/lab1_decorator/ex2/Rectangle.java
Normal file
16
src/lab1_decorator/ex2/Rectangle.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
5
src/lab1_decorator/ex2/Shape.java
Normal file
5
src/lab1_decorator/ex2/Shape.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab1_decorator.ex2;
|
||||||
|
|
||||||
|
public abstract class Shape {
|
||||||
|
public abstract void draw();
|
||||||
|
}
|
14
src/lab1_decorator/ex2/ShapeDecorator.java
Normal file
14
src/lab1_decorator/ex2/ShapeDecorator.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
39
src/lab1_decorator/ex2/ShapeDemo.java
Normal file
39
src/lab1_decorator/ex2/ShapeDemo.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
src/lab1_decorator/ex2/Square.java
Normal file
14
src/lab1_decorator/ex2/Square.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
16
src/lab1_decorator/ex2/TextureDecorator.java
Normal file
16
src/lab1_decorator/ex2/TextureDecorator.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user