added lab2 ex1

This commit is contained in:
Louis Heredero 2024-10-07 09:00:49 +02:00
parent 8fadc54ab2
commit 2f41073a0f
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
16 changed files with 143 additions and 0 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 265 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 155 KiB

View File

@ -0,0 +1,13 @@
package lab2_factory_method.ex1;
public abstract class Car {
private String color;
public Car(String color) {
this.color = color;
}
public abstract String getName();
public String getColor() {
return color;
};
}

View File

@ -0,0 +1,5 @@
package lab2_factory_method.ex1;
public abstract class CarCreator {
public abstract Car orderCar(String color);
}

View File

@ -0,0 +1,11 @@
package lab2_factory_method.ex1;
public class Coupe extends Car {
public Coupe(String color) {
super(color);
}
public String getName() {
return "Coupe";
}
}

View File

@ -0,0 +1,9 @@
package lab2_factory_method.ex1;
public class CoupeCreator extends CarCreator {
@Override
public Car orderCar(String color) {
return new Coupe(color);
}
}

View File

@ -0,0 +1,12 @@
package lab2_factory_method.ex1;
public class Minivan extends Car {
public Minivan(String color) {
super(color);
}
@Override
public String getName() {
return "Minivan";
}
}

View File

@ -0,0 +1,8 @@
package lab2_factory_method.ex1;
public class MinivanCreator extends CarCreator {
@Override
public Car orderCar(String color) {
return new Minivan(color);
}
}

View File

@ -0,0 +1,12 @@
package lab2_factory_method.ex1;
public class Pickup extends Car {
public Pickup(String color) {
super(color);
}
@Override
public String getName() {
return "Pickup";
}
}

View File

@ -0,0 +1,8 @@
package lab2_factory_method.ex1;
public class PickupCreator extends CarCreator {
@Override
public Car orderCar(String color) {
return new Pickup(color);
}
}

View File

@ -0,0 +1,21 @@
package lab2_factory_method.ex1;
public class Program {
public static void main(String[] args) {
CarCreator coupeCreator = new CoupeCreator();
CarCreator minivanCreator = new MinivanCreator();
CarCreator pickupCreator = new PickupCreator();
Car c1 = coupeCreator.orderCar("red");
System.out.println("Car "
+ c1.getName()
+ " "
+ c1.getColor()
+ " has been ordered.");
Car c2 = minivanCreator.orderCar("red");
System.out.println("Car "
+ c2.getName()
+ " "
+ c2.getColor()
+ " has been ordered.");
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_factory_method;
public class ConcreteCreator extends Creator {
@Override
public Product factoryMethod() {
return new ConcreteProduct("Product");
}
}

View File

@ -0,0 +1,14 @@
package learn.simple_factory_method;
public class ConcreteProduct implements Product {
private String category;
public ConcreteProduct(String category) {
this.category = category;
}
@Override
public String toString() {
return "ConcreteProduct('" + category + "')";
}
}

View File

@ -0,0 +1,10 @@
package learn.simple_factory_method;
public abstract class Creator {
abstract public Product factoryMethod();
public void anOperation() {
Product product = this.factoryMethod();
System.out.println("Do something with product " + product);
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_factory_method;
public class Main {
public static void main(String[] args) {
Creator creator = new ConcreteCreator();
creator.anOperation();
}
}

View File

@ -0,0 +1,4 @@
package learn.simple_factory_method;
public interface Product {
}