From 1ab845574a9ae8b2156186587d59ac230c6d7a43 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 7 Oct 2024 10:37:10 +0200 Subject: [PATCH] added lab2 ex2 --- src/lab2_factory_method/ex1/Car.java | 12 ++++---- src/lab2_factory_method/ex1/CarCreator.java | 7 ++++- src/lab2_factory_method/ex1/Coupe.java | 5 +--- src/lab2_factory_method/ex1/CoupeCreator.java | 4 +-- src/lab2_factory_method/ex1/Minivan.java | 4 --- .../ex1/MinivanCreator.java | 4 +-- src/lab2_factory_method/ex1/Pickup.java | 4 --- .../ex1/PickupCreator.java | 4 +-- .../ex2/ArchitectOffice.java | 19 ++++++++++++ src/lab2_factory_method/ex2/BricksHouse.java | 13 +++++++++ .../ex2/BricksHouseCreator.java | 8 +++++ src/lab2_factory_method/ex2/GlassHouse.java | 13 +++++++++ .../ex2/GlassHouseCreator.java | 8 +++++ src/lab2_factory_method/ex2/House.java | 6 ++++ src/lab2_factory_method/ex2/HouseCreator.java | 5 ++++ src/lab2_factory_method/ex2/WoodHouse.java | 13 +++++++++ .../ex2/WoodHouseCreator.java | 8 +++++ src/learn/simple_abstract_factory/Client.java | 29 +++++++++++++++++++ 18 files changed, 142 insertions(+), 24 deletions(-) create mode 100644 src/lab2_factory_method/ex2/ArchitectOffice.java create mode 100644 src/lab2_factory_method/ex2/BricksHouse.java create mode 100644 src/lab2_factory_method/ex2/BricksHouseCreator.java create mode 100644 src/lab2_factory_method/ex2/GlassHouse.java create mode 100644 src/lab2_factory_method/ex2/GlassHouseCreator.java create mode 100644 src/lab2_factory_method/ex2/House.java create mode 100644 src/lab2_factory_method/ex2/HouseCreator.java create mode 100644 src/lab2_factory_method/ex2/WoodHouse.java create mode 100644 src/lab2_factory_method/ex2/WoodHouseCreator.java create mode 100644 src/learn/simple_abstract_factory/Client.java diff --git a/src/lab2_factory_method/ex1/Car.java b/src/lab2_factory_method/ex1/Car.java index 0016f7c..0806ce4 100644 --- a/src/lab2_factory_method/ex1/Car.java +++ b/src/lab2_factory_method/ex1/Car.java @@ -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); + } } diff --git a/src/lab2_factory_method/ex1/CarCreator.java b/src/lab2_factory_method/ex1/CarCreator.java index a1244a2..5e3079b 100644 --- a/src/lab2_factory_method/ex1/CarCreator.java +++ b/src/lab2_factory_method/ex1/CarCreator.java @@ -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(); } diff --git a/src/lab2_factory_method/ex1/Coupe.java b/src/lab2_factory_method/ex1/Coupe.java index 869a0c0..b9ab5bc 100644 --- a/src/lab2_factory_method/ex1/Coupe.java +++ b/src/lab2_factory_method/ex1/Coupe.java @@ -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"; } diff --git a/src/lab2_factory_method/ex1/CoupeCreator.java b/src/lab2_factory_method/ex1/CoupeCreator.java index de22852..eaac946 100644 --- a/src/lab2_factory_method/ex1/CoupeCreator.java +++ b/src/lab2_factory_method/ex1/CoupeCreator.java @@ -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(); } } diff --git a/src/lab2_factory_method/ex1/Minivan.java b/src/lab2_factory_method/ex1/Minivan.java index d9b9944..abc29b2 100644 --- a/src/lab2_factory_method/ex1/Minivan.java +++ b/src/lab2_factory_method/ex1/Minivan.java @@ -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"; diff --git a/src/lab2_factory_method/ex1/MinivanCreator.java b/src/lab2_factory_method/ex1/MinivanCreator.java index d7ae29e..ab35943 100644 --- a/src/lab2_factory_method/ex1/MinivanCreator.java +++ b/src/lab2_factory_method/ex1/MinivanCreator.java @@ -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(); } } diff --git a/src/lab2_factory_method/ex1/Pickup.java b/src/lab2_factory_method/ex1/Pickup.java index 5d50c87..018a895 100644 --- a/src/lab2_factory_method/ex1/Pickup.java +++ b/src/lab2_factory_method/ex1/Pickup.java @@ -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"; diff --git a/src/lab2_factory_method/ex1/PickupCreator.java b/src/lab2_factory_method/ex1/PickupCreator.java index b58b8b7..0a9ad48 100644 --- a/src/lab2_factory_method/ex1/PickupCreator.java +++ b/src/lab2_factory_method/ex1/PickupCreator.java @@ -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(); } } diff --git a/src/lab2_factory_method/ex2/ArchitectOffice.java b/src/lab2_factory_method/ex2/ArchitectOffice.java new file mode 100644 index 0000000..cbdceb6 --- /dev/null +++ b/src/lab2_factory_method/ex2/ArchitectOffice.java @@ -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(); + } +} diff --git a/src/lab2_factory_method/ex2/BricksHouse.java b/src/lab2_factory_method/ex2/BricksHouse.java new file mode 100644 index 0000000..212474b --- /dev/null +++ b/src/lab2_factory_method/ex2/BricksHouse.java @@ -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"); + } +} diff --git a/src/lab2_factory_method/ex2/BricksHouseCreator.java b/src/lab2_factory_method/ex2/BricksHouseCreator.java new file mode 100644 index 0000000..b37d16a --- /dev/null +++ b/src/lab2_factory_method/ex2/BricksHouseCreator.java @@ -0,0 +1,8 @@ +package lab2_factory_method.ex2; + +public class BricksHouseCreator extends HouseCreator { + @Override + public House create() { + return new BricksHouse(); + } +} diff --git a/src/lab2_factory_method/ex2/GlassHouse.java b/src/lab2_factory_method/ex2/GlassHouse.java new file mode 100644 index 0000000..28c22ef --- /dev/null +++ b/src/lab2_factory_method/ex2/GlassHouse.java @@ -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"); + } +} diff --git a/src/lab2_factory_method/ex2/GlassHouseCreator.java b/src/lab2_factory_method/ex2/GlassHouseCreator.java new file mode 100644 index 0000000..fab1382 --- /dev/null +++ b/src/lab2_factory_method/ex2/GlassHouseCreator.java @@ -0,0 +1,8 @@ +package lab2_factory_method.ex2; + +public class GlassHouseCreator extends HouseCreator { + @Override + public House create() { + return new GlassHouse(); + } +} diff --git a/src/lab2_factory_method/ex2/House.java b/src/lab2_factory_method/ex2/House.java new file mode 100644 index 0000000..546a7ad --- /dev/null +++ b/src/lab2_factory_method/ex2/House.java @@ -0,0 +1,6 @@ +package lab2_factory_method.ex2; + +public abstract class House { + public abstract void buildHouse(); + public abstract void calculatePrice(); +} diff --git a/src/lab2_factory_method/ex2/HouseCreator.java b/src/lab2_factory_method/ex2/HouseCreator.java new file mode 100644 index 0000000..728445b --- /dev/null +++ b/src/lab2_factory_method/ex2/HouseCreator.java @@ -0,0 +1,5 @@ +package lab2_factory_method.ex2; + +public abstract class HouseCreator { + public abstract House create(); +} diff --git a/src/lab2_factory_method/ex2/WoodHouse.java b/src/lab2_factory_method/ex2/WoodHouse.java new file mode 100644 index 0000000..1365314 --- /dev/null +++ b/src/lab2_factory_method/ex2/WoodHouse.java @@ -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"); + } +} diff --git a/src/lab2_factory_method/ex2/WoodHouseCreator.java b/src/lab2_factory_method/ex2/WoodHouseCreator.java new file mode 100644 index 0000000..61e0f4a --- /dev/null +++ b/src/lab2_factory_method/ex2/WoodHouseCreator.java @@ -0,0 +1,8 @@ +package lab2_factory_method.ex2; + +public class WoodHouseCreator extends HouseCreator { + @Override + public House create() { + return new WoodHouse(); + } +} diff --git a/src/learn/simple_abstract_factory/Client.java b/src/learn/simple_abstract_factory/Client.java new file mode 100644 index 0000000..7798860 --- /dev/null +++ b/src/learn/simple_abstract_factory/Client.java @@ -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 + + '}'; + } +}