added lab3 ex2

This commit is contained in:
Louis Heredero 2024-10-07 11:14:07 +02:00
parent 5fbcd72ea6
commit 7c3d0b2cbd
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
10 changed files with 132 additions and 0 deletions

View File

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

View File

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

View File

@ -0,0 +1,5 @@
package lab3_abstract_factory.ex2;
public abstract class CarFactory {
public abstract Car createCar();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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