Compare commits
3 Commits
075524c606
...
1f3f189adf
| Author | SHA1 | Date | |
|---|---|---|---|
|
1f3f189adf
|
|||
|
206843abb1
|
|||
|
f2fb3f7e47
|
22
src/lab1_decorator/ex1/AirbagDecorator.java
Normal file
22
src/lab1_decorator/ex1/AirbagDecorator.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class AirbagDecorator extends CarDecorator {
|
||||||
|
public AirbagDecorator(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 10 + super.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 10 + super.getSecurityLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return super.getDescription() + ", Airbag System";
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java
Normal file
22
src/lab1_decorator/ex1/AntiSlidingSystemDecorator.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class AntiSlidingSystemDecorator extends CarDecorator {
|
||||||
|
public AntiSlidingSystemDecorator(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 20 + super.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 5 + super.getSecurityLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return super.getDescription() + ", Anti Sliding System";
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/lab1_decorator/ex1/BreakingSystemDecorator.java
Normal file
22
src/lab1_decorator/ex1/BreakingSystemDecorator.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class BreakingSystemDecorator extends CarDecorator {
|
||||||
|
public BreakingSystemDecorator(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 6 + super.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 8 + super.getSecurityLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return super.getDescription() + ", Breaking System";
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/lab1_decorator/ex1/Car.java
Normal file
7
src/lab1_decorator/ex1/Car.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public interface Car {
|
||||||
|
String getDescription();
|
||||||
|
double getCost();
|
||||||
|
double getSecurityLevel();
|
||||||
|
}
|
||||||
18
src/lab1_decorator/ex1/CarBerlin.java
Normal file
18
src/lab1_decorator/ex1/CarBerlin.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class CarBerlin implements Car {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "A beautiful Berlin";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/lab1_decorator/ex1/CarBreak.java
Normal file
18
src/lab1_decorator/ex1/CarBreak.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class CarBreak implements Car {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "A family break";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
24
src/lab1_decorator/ex1/CarDecorator.java
Normal file
24
src/lab1_decorator/ex1/CarDecorator.java
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public abstract class CarDecorator implements Car {
|
||||||
|
public Car car;
|
||||||
|
public String name;
|
||||||
|
|
||||||
|
public CarDecorator(Car car) {
|
||||||
|
this.car = car;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDescription() {
|
||||||
|
return car.getDescription();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return car.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return car.getSecurityLevel();
|
||||||
|
}
|
||||||
|
}
|
||||||
38
src/lab1_decorator/ex1/CarDecoratorLauncher.java
Normal file
38
src/lab1_decorator/ex1/CarDecoratorLauncher.java
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class CarDecoratorLauncher {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CarDecoratorLauncher launcher = new CarDecoratorLauncher();
|
||||||
|
launcher.launch();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void launch() {
|
||||||
|
Car car1 = new CarBerlin();
|
||||||
|
car1 = new AirbagDecorator(car1);
|
||||||
|
car1 = new AirbagDecorator(car1);
|
||||||
|
car1 = new EngineDecorator(car1);
|
||||||
|
car1 = new ColorDecorator(car1);
|
||||||
|
printCarInfos(car1);
|
||||||
|
|
||||||
|
Car car2 = new CarSport();
|
||||||
|
car2 = new AntiSlidingSystemDecorator(car2);
|
||||||
|
car2 = new BreakingSystemDecorator(car2);
|
||||||
|
printCarInfos(car2);
|
||||||
|
|
||||||
|
Car car3 = new CarBreak();
|
||||||
|
car3 = new AirbagDecorator(car3);
|
||||||
|
car3 = new EngineDecorator(car3);
|
||||||
|
car3 = new ColorDecorator(car3);
|
||||||
|
car3 = new AntiSlidingSystemDecorator(car3);
|
||||||
|
car3 = new BreakingSystemDecorator(car3);
|
||||||
|
printCarInfos(car3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printCarInfos(Car car) {
|
||||||
|
System.out.println("Car description : " + car.getDescription());
|
||||||
|
System.out.println("Car cost : " + car.getCost());
|
||||||
|
System.out.println("Car security level : " + car.getSecurityLevel());
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/lab1_decorator/ex1/CarSport.java
Normal file
18
src/lab1_decorator/ex1/CarSport.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class CarSport implements Car {
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return "A robust sport car";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 300;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getSecurityLevel() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/lab1_decorator/ex1/ColorDecorator.java
Normal file
18
src/lab1_decorator/ex1/ColorDecorator.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
|
||||||
|
public class ColorDecorator extends CarDecorator {
|
||||||
|
public ColorDecorator(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 3 + super.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return super.getDescription() + ", Color";
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/lab1_decorator/ex1/EngineDecorator.java
Normal file
17
src/lab1_decorator/ex1/EngineDecorator.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package lab1_decorator.ex1;
|
||||||
|
|
||||||
|
public class EngineDecorator extends CarDecorator {
|
||||||
|
public EngineDecorator(Car car) {
|
||||||
|
super(car);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double getCost() {
|
||||||
|
return 20 + super.getCost();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return super.getDescription() + ", Engine";
|
||||||
|
}
|
||||||
|
}
|
||||||
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/learn/simple_decorator/Component.java
Normal file
5
src/learn/simple_decorator/Component.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public interface Component {
|
||||||
|
void operation();
|
||||||
|
}
|
||||||
8
src/learn/simple_decorator/ConcreteComponent.java
Normal file
8
src/learn/simple_decorator/ConcreteComponent.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public class ConcreteComponent implements Component {
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
System.out.println("Concrete operation");
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/learn/simple_decorator/ConcreteDecoratorA.java
Normal file
13
src/learn/simple_decorator/ConcreteDecoratorA.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public class ConcreteDecoratorA extends Decorator {
|
||||||
|
public ConcreteDecoratorA(Component component) {
|
||||||
|
super(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
System.out.println("I am adding some new responsibilities here.");
|
||||||
|
super.operation();
|
||||||
|
}
|
||||||
|
}
|
||||||
17
src/learn/simple_decorator/ConcreteDecoratorB.java
Normal file
17
src/learn/simple_decorator/ConcreteDecoratorB.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public class ConcreteDecoratorB extends Decorator {
|
||||||
|
public ConcreteDecoratorB(Component component) {
|
||||||
|
super(component);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
addedBehavior();
|
||||||
|
super.operation();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addedBehavior() {
|
||||||
|
System.out.println("I am adding some new behavior here.");
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/learn/simple_decorator/Decorator.java
Normal file
14
src/learn/simple_decorator/Decorator.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public abstract class Decorator implements Component {
|
||||||
|
private Component component;
|
||||||
|
|
||||||
|
public Decorator(Component component) {
|
||||||
|
this.component = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void operation() {
|
||||||
|
component.operation();
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/learn/simple_decorator/Main.java
Normal file
11
src/learn/simple_decorator/Main.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package learn.simple_decorator;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Component component = new ConcreteComponent();
|
||||||
|
component = new ConcreteDecoratorB(component);
|
||||||
|
component = new ConcreteDecoratorA(component);
|
||||||
|
|
||||||
|
component.operation();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user