added lab2 ex2

This commit is contained in:
Louis Heredero 2024-10-07 10:37:10 +02:00
parent 50e6ed18df
commit 1ab845574a
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
18 changed files with 142 additions and 24 deletions

View File

@ -1,13 +1,15 @@
package lab2_factory_method.ex1;
public abstract class Car {
private String color;
private String color = "blank";
public Car(String color) {
this.color = color;
}
public Car() {}
public abstract String getName();
public String getColor() {
return color;
};
}
public void paintColor(String color) {
this.color = color;
System.out.println("Painting car in " + color);
}
}

View File

@ -1,5 +1,10 @@
package lab2_factory_method.ex1;
public abstract class CarCreator {
public abstract Car orderCar(String color);
public Car orderCar(String color) {
Car car = createCar();
car.paintColor(color);
return car;
}
public abstract Car createCar();
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,19 @@
package lab2_factory_method.ex2;
public class ArchitectOffice {
public static void main(String[] args) {
HouseCreator glassHouseCreator = new GlassHouseCreator();
HouseCreator bricksHouseCreator = new BricksHouseCreator();
HouseCreator woodHouseCreator = new WoodHouseCreator();
House house1 = glassHouseCreator.create();
house1.buildHouse();
house1.calculatePrice();
House house2 = bricksHouseCreator.create();
house2.buildHouse();
house2.calculatePrice();
House house3 = woodHouseCreator.create();
house3.buildHouse();
house3.calculatePrice();
}
}

View File

@ -0,0 +1,13 @@
package lab2_factory_method.ex2;
public class BricksHouse extends House {
@Override
public void buildHouse() {
System.out.println("Building a bricks house");
}
@Override
public void calculatePrice() {
System.out.println("Modest bricks house");
}
}

View File

@ -0,0 +1,8 @@
package lab2_factory_method.ex2;
public class BricksHouseCreator extends HouseCreator {
@Override
public House create() {
return new BricksHouse();
}
}

View File

@ -0,0 +1,13 @@
package lab2_factory_method.ex2;
public class GlassHouse extends House {
@Override
public void buildHouse() {
System.out.println("Building a glass house");
}
@Override
public void calculatePrice() {
System.out.println("Expensive glass house");
}
}

View File

@ -0,0 +1,8 @@
package lab2_factory_method.ex2;
public class GlassHouseCreator extends HouseCreator {
@Override
public House create() {
return new GlassHouse();
}
}

View File

@ -0,0 +1,6 @@
package lab2_factory_method.ex2;
public abstract class House {
public abstract void buildHouse();
public abstract void calculatePrice();
}

View File

@ -0,0 +1,5 @@
package lab2_factory_method.ex2;
public abstract class HouseCreator {
public abstract House create();
}

View File

@ -0,0 +1,13 @@
package lab2_factory_method.ex2;
public class WoodHouse extends House {
@Override
public void buildHouse() {
System.out.println("Building a wooden house");
}
@Override
public void calculatePrice() {
System.out.println("Mid range wooden chalet");
}
}

View File

@ -0,0 +1,8 @@
package lab2_factory_method.ex2;
public class WoodHouseCreator extends HouseCreator {
@Override
public House create() {
return new WoodHouse();
}
}

View File

@ -0,0 +1,29 @@
package learn.simple_abstract_factory;
public class Client {
private AbstractFactory abstractFactory;
private AbstractProductA productA;
private AbstractProductB productB;
public Client(AbstractFactory abstractFactory) {
this.abstractFactory = abstractFactory;
}
public void setup() {
productA = abstractFactory.createProductA();
productB = abstractFactory.createProductB();
}
public void setAbstractFactory(AbstractFactory abstractFactory) {
this.abstractFactory = abstractFactory;
}
@Override
public String toString() {
return "Client{" +
"productA=" + productA +
", productB=" + productB +
'}';
}
}