added lab2 ex2
This commit is contained in:
parent
50e6ed18df
commit
1ab845574a
@ -1,13 +1,15 @@
|
|||||||
package lab2_factory_method.ex1;
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
public abstract class Car {
|
public abstract class Car {
|
||||||
private String color;
|
private String color = "blank";
|
||||||
|
|
||||||
public Car(String color) {
|
public Car() {}
|
||||||
this.color = color;
|
|
||||||
}
|
|
||||||
public abstract String getName();
|
public abstract String getName();
|
||||||
public String getColor() {
|
public String getColor() {
|
||||||
return color;
|
return color;
|
||||||
};
|
}
|
||||||
|
public void paintColor(String color) {
|
||||||
|
this.color = color;
|
||||||
|
System.out.println("Painting car in " + color);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
package lab2_factory_method.ex1;
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
public abstract class CarCreator {
|
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();
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,7 @@
|
|||||||
package lab2_factory_method.ex1;
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
public class Coupe extends Car {
|
public class Coupe extends Car {
|
||||||
public Coupe(String color) {
|
@Override
|
||||||
super(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "Coupe";
|
return "Coupe";
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package lab2_factory_method.ex1;
|
|||||||
public class CoupeCreator extends CarCreator {
|
public class CoupeCreator extends CarCreator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Car orderCar(String color) {
|
public Car createCar() {
|
||||||
return new Coupe(color);
|
return new Coupe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package lab2_factory_method.ex1;
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
public class Minivan extends Car {
|
public class Minivan extends Car {
|
||||||
public Minivan(String color) {
|
|
||||||
super(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "Minivan";
|
return "Minivan";
|
||||||
|
@ -2,7 +2,7 @@ package lab2_factory_method.ex1;
|
|||||||
|
|
||||||
public class MinivanCreator extends CarCreator {
|
public class MinivanCreator extends CarCreator {
|
||||||
@Override
|
@Override
|
||||||
public Car orderCar(String color) {
|
public Car createCar() {
|
||||||
return new Minivan(color);
|
return new Minivan();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,6 @@
|
|||||||
package lab2_factory_method.ex1;
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
public class Pickup extends Car {
|
public class Pickup extends Car {
|
||||||
public Pickup(String color) {
|
|
||||||
super(color);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "Pickup";
|
return "Pickup";
|
||||||
|
@ -2,7 +2,7 @@ package lab2_factory_method.ex1;
|
|||||||
|
|
||||||
public class PickupCreator extends CarCreator {
|
public class PickupCreator extends CarCreator {
|
||||||
@Override
|
@Override
|
||||||
public Car orderCar(String color) {
|
public Car createCar() {
|
||||||
return new Pickup(color);
|
return new Pickup();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
src/lab2_factory_method/ex2/ArchitectOffice.java
Normal file
19
src/lab2_factory_method/ex2/ArchitectOffice.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
13
src/lab2_factory_method/ex2/BricksHouse.java
Normal file
13
src/lab2_factory_method/ex2/BricksHouse.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
8
src/lab2_factory_method/ex2/BricksHouseCreator.java
Normal file
8
src/lab2_factory_method/ex2/BricksHouseCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab2_factory_method.ex2;
|
||||||
|
|
||||||
|
public class BricksHouseCreator extends HouseCreator {
|
||||||
|
@Override
|
||||||
|
public House create() {
|
||||||
|
return new BricksHouse();
|
||||||
|
}
|
||||||
|
}
|
13
src/lab2_factory_method/ex2/GlassHouse.java
Normal file
13
src/lab2_factory_method/ex2/GlassHouse.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
8
src/lab2_factory_method/ex2/GlassHouseCreator.java
Normal file
8
src/lab2_factory_method/ex2/GlassHouseCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab2_factory_method.ex2;
|
||||||
|
|
||||||
|
public class GlassHouseCreator extends HouseCreator {
|
||||||
|
@Override
|
||||||
|
public House create() {
|
||||||
|
return new GlassHouse();
|
||||||
|
}
|
||||||
|
}
|
6
src/lab2_factory_method/ex2/House.java
Normal file
6
src/lab2_factory_method/ex2/House.java
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package lab2_factory_method.ex2;
|
||||||
|
|
||||||
|
public abstract class House {
|
||||||
|
public abstract void buildHouse();
|
||||||
|
public abstract void calculatePrice();
|
||||||
|
}
|
5
src/lab2_factory_method/ex2/HouseCreator.java
Normal file
5
src/lab2_factory_method/ex2/HouseCreator.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab2_factory_method.ex2;
|
||||||
|
|
||||||
|
public abstract class HouseCreator {
|
||||||
|
public abstract House create();
|
||||||
|
}
|
13
src/lab2_factory_method/ex2/WoodHouse.java
Normal file
13
src/lab2_factory_method/ex2/WoodHouse.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
8
src/lab2_factory_method/ex2/WoodHouseCreator.java
Normal file
8
src/lab2_factory_method/ex2/WoodHouseCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab2_factory_method.ex2;
|
||||||
|
|
||||||
|
public class WoodHouseCreator extends HouseCreator {
|
||||||
|
@Override
|
||||||
|
public House create() {
|
||||||
|
return new WoodHouse();
|
||||||
|
}
|
||||||
|
}
|
29
src/learn/simple_abstract_factory/Client.java
Normal file
29
src/learn/simple_abstract_factory/Client.java
Normal 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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user