added lab3 ex2
This commit is contained in:
parent
5fbcd72ea6
commit
7c3d0b2cbd
18
src/lab3_abstract_factory/ex2/Car.java
Normal file
18
src/lab3_abstract_factory/ex2/Car.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package lab3_abstract_factory.ex2;
|
||||||
|
|
||||||
|
public abstract class Car {
|
||||||
|
protected String name;
|
||||||
|
private String color = "blank";
|
||||||
|
|
||||||
|
public void paintColor(String color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
}
|
15
src/lab3_abstract_factory/ex2/CarCreator.java
Normal file
15
src/lab3_abstract_factory/ex2/CarCreator.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package lab3_abstract_factory.ex2;
|
||||||
|
|
||||||
|
public class CarCreator {
|
||||||
|
private CarFactory carFactory;
|
||||||
|
|
||||||
|
public CarCreator(CarFactory carFactory) {
|
||||||
|
this.carFactory = carFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Car orderCar(String color) {
|
||||||
|
Car car = carFactory.createCar();
|
||||||
|
car.paintColor(color);
|
||||||
|
return car;
|
||||||
|
}
|
||||||
|
}
|
5
src/lab3_abstract_factory/ex2/CarFactory.java
Normal file
5
src/lab3_abstract_factory/ex2/CarFactory.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab3_abstract_factory.ex2;
|
||||||
|
|
||||||
|
public abstract class CarFactory {
|
||||||
|
public abstract Car createCar();
|
||||||
|
}
|
31
src/lab3_abstract_factory/ex2/Program.java
Normal file
31
src/lab3_abstract_factory/ex2/Program.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
package lab3_abstract_factory.ex2;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.car_factories.CoupeFactory;
|
||||||
|
import lab3_abstract_factory.ex2.car_factories.MinivanFactory;
|
||||||
|
import lab3_abstract_factory.ex2.car_factories.PickupFactory;
|
||||||
|
|
||||||
|
public class Program {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CarCreator coupeCreator = new CarCreator(new CoupeFactory());
|
||||||
|
CarCreator minivanCreator = new CarCreator(new MinivanFactory());
|
||||||
|
CarCreator pickupCreator = new CarCreator(new PickupFactory());
|
||||||
|
|
||||||
|
Car c1 = coupeCreator.orderCar("red");
|
||||||
|
System.out.println("Car "
|
||||||
|
+ c1.getName() + " "
|
||||||
|
+ c1.getColor()
|
||||||
|
+ " has been ordered.");
|
||||||
|
|
||||||
|
Car c2 = minivanCreator.orderCar("blue");
|
||||||
|
System.out.println("Car "
|
||||||
|
+ c2.getName() + " "
|
||||||
|
+ c2.getColor()
|
||||||
|
+ " has been ordered.");
|
||||||
|
|
||||||
|
Car c3 = pickupCreator.orderCar("green");
|
||||||
|
System.out.println("Car "
|
||||||
|
+ c3.getName() + " "
|
||||||
|
+ c3.getColor()
|
||||||
|
+ " has been ordered.");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package lab3_abstract_factory.ex2.car_factories;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
import lab3_abstract_factory.ex2.CarFactory;
|
||||||
|
import lab3_abstract_factory.ex2.cars.Coupe;
|
||||||
|
|
||||||
|
public class CoupeFactory extends CarFactory {
|
||||||
|
@Override
|
||||||
|
public Car createCar() {
|
||||||
|
return new Coupe();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package lab3_abstract_factory.ex2.car_factories;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
import lab3_abstract_factory.ex2.CarFactory;
|
||||||
|
import lab3_abstract_factory.ex2.cars.Minivan;
|
||||||
|
|
||||||
|
public class MinivanFactory extends CarFactory {
|
||||||
|
@Override
|
||||||
|
public Car createCar() {
|
||||||
|
return new Minivan();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package lab3_abstract_factory.ex2.car_factories;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
import lab3_abstract_factory.ex2.CarFactory;
|
||||||
|
import lab3_abstract_factory.ex2.cars.Pickup;
|
||||||
|
|
||||||
|
public class PickupFactory extends CarFactory {
|
||||||
|
@Override
|
||||||
|
public Car createCar() {
|
||||||
|
return new Pickup();
|
||||||
|
}
|
||||||
|
}
|
9
src/lab3_abstract_factory/ex2/cars/Coupe.java
Normal file
9
src/lab3_abstract_factory/ex2/cars/Coupe.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package lab3_abstract_factory.ex2.cars;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
|
||||||
|
public class Coupe extends Car {
|
||||||
|
public Coupe() {
|
||||||
|
this.name = "Coupe";
|
||||||
|
}
|
||||||
|
}
|
9
src/lab3_abstract_factory/ex2/cars/Minivan.java
Normal file
9
src/lab3_abstract_factory/ex2/cars/Minivan.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package lab3_abstract_factory.ex2.cars;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
|
||||||
|
public class Minivan extends Car {
|
||||||
|
public Minivan() {
|
||||||
|
this.name = "Minivan";
|
||||||
|
}
|
||||||
|
}
|
9
src/lab3_abstract_factory/ex2/cars/Pickup.java
Normal file
9
src/lab3_abstract_factory/ex2/cars/Pickup.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package lab3_abstract_factory.ex2.cars;
|
||||||
|
|
||||||
|
import lab3_abstract_factory.ex2.Car;
|
||||||
|
|
||||||
|
public class Pickup extends Car {
|
||||||
|
public Pickup() {
|
||||||
|
this.name = "Pickup";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user