added lab2 ex2
This commit is contained in:
parent
50e6ed18df
commit
1ab845574a
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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";
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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";
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
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