diff --git a/src/lab3_abstract_factory/ex2/Car.java b/src/lab3_abstract_factory/ex2/Car.java new file mode 100644 index 0000000..6c6466b --- /dev/null +++ b/src/lab3_abstract_factory/ex2/Car.java @@ -0,0 +1,18 @@ +package lab3_abstract_factory.ex2; + +public abstract class Car { + protected String name; + private String color = "blank"; + + public void paintColor(String color) { + this.color = color; + } + + public String getColor() { + return color; + } + + public String getName() { + return name; + } +} diff --git a/src/lab3_abstract_factory/ex2/CarCreator.java b/src/lab3_abstract_factory/ex2/CarCreator.java new file mode 100644 index 0000000..13f0a15 --- /dev/null +++ b/src/lab3_abstract_factory/ex2/CarCreator.java @@ -0,0 +1,15 @@ +package lab3_abstract_factory.ex2; + +public class CarCreator { + private CarFactory carFactory; + + public CarCreator(CarFactory carFactory) { + this.carFactory = carFactory; + } + + public Car orderCar(String color) { + Car car = carFactory.createCar(); + car.paintColor(color); + return car; + } +} diff --git a/src/lab3_abstract_factory/ex2/CarFactory.java b/src/lab3_abstract_factory/ex2/CarFactory.java new file mode 100644 index 0000000..a3bfe6b --- /dev/null +++ b/src/lab3_abstract_factory/ex2/CarFactory.java @@ -0,0 +1,5 @@ +package lab3_abstract_factory.ex2; + +public abstract class CarFactory { + public abstract Car createCar(); +} diff --git a/src/lab3_abstract_factory/ex2/Program.java b/src/lab3_abstract_factory/ex2/Program.java new file mode 100644 index 0000000..c9a32e1 --- /dev/null +++ b/src/lab3_abstract_factory/ex2/Program.java @@ -0,0 +1,31 @@ +package lab3_abstract_factory.ex2; + +import lab3_abstract_factory.ex2.car_factories.CoupeFactory; +import lab3_abstract_factory.ex2.car_factories.MinivanFactory; +import lab3_abstract_factory.ex2.car_factories.PickupFactory; + +public class Program { + public static void main(String[] args) { + CarCreator coupeCreator = new CarCreator(new CoupeFactory()); + CarCreator minivanCreator = new CarCreator(new MinivanFactory()); + CarCreator pickupCreator = new CarCreator(new PickupFactory()); + + Car c1 = coupeCreator.orderCar("red"); + System.out.println("Car " + + c1.getName() + " " + + c1.getColor() + + " has been ordered."); + + Car c2 = minivanCreator.orderCar("blue"); + System.out.println("Car " + + c2.getName() + " " + + c2.getColor() + + " has been ordered."); + + Car c3 = pickupCreator.orderCar("green"); + System.out.println("Car " + + c3.getName() + " " + + c3.getColor() + + " has been ordered."); + } +} \ No newline at end of file diff --git a/src/lab3_abstract_factory/ex2/car_factories/CoupeFactory.java b/src/lab3_abstract_factory/ex2/car_factories/CoupeFactory.java new file mode 100644 index 0000000..3e65991 --- /dev/null +++ b/src/lab3_abstract_factory/ex2/car_factories/CoupeFactory.java @@ -0,0 +1,12 @@ +package lab3_abstract_factory.ex2.car_factories; + +import lab3_abstract_factory.ex2.Car; +import lab3_abstract_factory.ex2.CarFactory; +import lab3_abstract_factory.ex2.cars.Coupe; + +public class CoupeFactory extends CarFactory { + @Override + public Car createCar() { + return new Coupe(); + } +} diff --git a/src/lab3_abstract_factory/ex2/car_factories/MinivanFactory.java b/src/lab3_abstract_factory/ex2/car_factories/MinivanFactory.java new file mode 100644 index 0000000..c98df35 --- /dev/null +++ b/src/lab3_abstract_factory/ex2/car_factories/MinivanFactory.java @@ -0,0 +1,12 @@ +package lab3_abstract_factory.ex2.car_factories; + +import lab3_abstract_factory.ex2.Car; +import lab3_abstract_factory.ex2.CarFactory; +import lab3_abstract_factory.ex2.cars.Minivan; + +public class MinivanFactory extends CarFactory { + @Override + public Car createCar() { + return new Minivan(); + } +} diff --git a/src/lab3_abstract_factory/ex2/car_factories/PickupFactory.java b/src/lab3_abstract_factory/ex2/car_factories/PickupFactory.java new file mode 100644 index 0000000..b60126e --- /dev/null +++ b/src/lab3_abstract_factory/ex2/car_factories/PickupFactory.java @@ -0,0 +1,12 @@ +package lab3_abstract_factory.ex2.car_factories; + +import lab3_abstract_factory.ex2.Car; +import lab3_abstract_factory.ex2.CarFactory; +import lab3_abstract_factory.ex2.cars.Pickup; + +public class PickupFactory extends CarFactory { + @Override + public Car createCar() { + return new Pickup(); + } +} diff --git a/src/lab3_abstract_factory/ex2/cars/Coupe.java b/src/lab3_abstract_factory/ex2/cars/Coupe.java new file mode 100644 index 0000000..3e0b3be --- /dev/null +++ b/src/lab3_abstract_factory/ex2/cars/Coupe.java @@ -0,0 +1,9 @@ +package lab3_abstract_factory.ex2.cars; + +import lab3_abstract_factory.ex2.Car; + +public class Coupe extends Car { + public Coupe() { + this.name = "Coupe"; + } +} diff --git a/src/lab3_abstract_factory/ex2/cars/Minivan.java b/src/lab3_abstract_factory/ex2/cars/Minivan.java new file mode 100644 index 0000000..66868cc --- /dev/null +++ b/src/lab3_abstract_factory/ex2/cars/Minivan.java @@ -0,0 +1,9 @@ +package lab3_abstract_factory.ex2.cars; + +import lab3_abstract_factory.ex2.Car; + +public class Minivan extends Car { + public Minivan() { + this.name = "Minivan"; + } +} diff --git a/src/lab3_abstract_factory/ex2/cars/Pickup.java b/src/lab3_abstract_factory/ex2/cars/Pickup.java new file mode 100644 index 0000000..08e6924 --- /dev/null +++ b/src/lab3_abstract_factory/ex2/cars/Pickup.java @@ -0,0 +1,9 @@ +package lab3_abstract_factory.ex2.cars; + +import lab3_abstract_factory.ex2.Car; + +public class Pickup extends Car { + public Pickup() { + this.name = "Pickup"; + } +}