added lab2 ex1
This commit is contained in:
parent
8fadc54ab2
commit
2f41073a0f
Binary file not shown.
Before Width: | Height: | Size: 265 KiB |
Binary file not shown.
Before Width: | Height: | Size: 155 KiB |
13
src/lab2_factory_method/ex1/Car.java
Normal file
13
src/lab2_factory_method/ex1/Car.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public abstract class Car {
|
||||||
|
private String color;
|
||||||
|
|
||||||
|
public Car(String color) {
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
public abstract String getName();
|
||||||
|
public String getColor() {
|
||||||
|
return color;
|
||||||
|
};
|
||||||
|
}
|
5
src/lab2_factory_method/ex1/CarCreator.java
Normal file
5
src/lab2_factory_method/ex1/CarCreator.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public abstract class CarCreator {
|
||||||
|
public abstract Car orderCar(String color);
|
||||||
|
}
|
11
src/lab2_factory_method/ex1/Coupe.java
Normal file
11
src/lab2_factory_method/ex1/Coupe.java
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class Coupe extends Car {
|
||||||
|
public Coupe(String color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return "Coupe";
|
||||||
|
}
|
||||||
|
}
|
9
src/lab2_factory_method/ex1/CoupeCreator.java
Normal file
9
src/lab2_factory_method/ex1/CoupeCreator.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class CoupeCreator extends CarCreator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Car orderCar(String color) {
|
||||||
|
return new Coupe(color);
|
||||||
|
}
|
||||||
|
}
|
12
src/lab2_factory_method/ex1/Minivan.java
Normal file
12
src/lab2_factory_method/ex1/Minivan.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class Minivan extends Car {
|
||||||
|
public Minivan(String color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Minivan";
|
||||||
|
}
|
||||||
|
}
|
8
src/lab2_factory_method/ex1/MinivanCreator.java
Normal file
8
src/lab2_factory_method/ex1/MinivanCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class MinivanCreator extends CarCreator {
|
||||||
|
@Override
|
||||||
|
public Car orderCar(String color) {
|
||||||
|
return new Minivan(color);
|
||||||
|
}
|
||||||
|
}
|
12
src/lab2_factory_method/ex1/Pickup.java
Normal file
12
src/lab2_factory_method/ex1/Pickup.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class Pickup extends Car {
|
||||||
|
public Pickup(String color) {
|
||||||
|
super(color);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getName() {
|
||||||
|
return "Pickup";
|
||||||
|
}
|
||||||
|
}
|
8
src/lab2_factory_method/ex1/PickupCreator.java
Normal file
8
src/lab2_factory_method/ex1/PickupCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class PickupCreator extends CarCreator {
|
||||||
|
@Override
|
||||||
|
public Car orderCar(String color) {
|
||||||
|
return new Pickup(color);
|
||||||
|
}
|
||||||
|
}
|
21
src/lab2_factory_method/ex1/Program.java
Normal file
21
src/lab2_factory_method/ex1/Program.java
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
package lab2_factory_method.ex1;
|
||||||
|
|
||||||
|
public class Program {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CarCreator coupeCreator = new CoupeCreator();
|
||||||
|
CarCreator minivanCreator = new MinivanCreator();
|
||||||
|
CarCreator pickupCreator = new PickupCreator();
|
||||||
|
Car c1 = coupeCreator.orderCar("red");
|
||||||
|
System.out.println("Car "
|
||||||
|
+ c1.getName()
|
||||||
|
+ " "
|
||||||
|
+ c1.getColor()
|
||||||
|
+ " has been ordered.");
|
||||||
|
Car c2 = minivanCreator.orderCar("red");
|
||||||
|
System.out.println("Car "
|
||||||
|
+ c2.getName()
|
||||||
|
+ " "
|
||||||
|
+ c2.getColor()
|
||||||
|
+ " has been ordered.");
|
||||||
|
}
|
||||||
|
}
|
8
src/learn/simple_factory_method/ConcreteCreator.java
Normal file
8
src/learn/simple_factory_method/ConcreteCreator.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_factory_method;
|
||||||
|
|
||||||
|
public class ConcreteCreator extends Creator {
|
||||||
|
@Override
|
||||||
|
public Product factoryMethod() {
|
||||||
|
return new ConcreteProduct("Product");
|
||||||
|
}
|
||||||
|
}
|
14
src/learn/simple_factory_method/ConcreteProduct.java
Normal file
14
src/learn/simple_factory_method/ConcreteProduct.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package learn.simple_factory_method;
|
||||||
|
|
||||||
|
public class ConcreteProduct implements Product {
|
||||||
|
private String category;
|
||||||
|
|
||||||
|
public ConcreteProduct(String category) {
|
||||||
|
this.category = category;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ConcreteProduct('" + category + "')";
|
||||||
|
}
|
||||||
|
}
|
10
src/learn/simple_factory_method/Creator.java
Normal file
10
src/learn/simple_factory_method/Creator.java
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
package learn.simple_factory_method;
|
||||||
|
|
||||||
|
public abstract class Creator {
|
||||||
|
abstract public Product factoryMethod();
|
||||||
|
|
||||||
|
public void anOperation() {
|
||||||
|
Product product = this.factoryMethod();
|
||||||
|
System.out.println("Do something with product " + product);
|
||||||
|
}
|
||||||
|
}
|
8
src/learn/simple_factory_method/Main.java
Normal file
8
src/learn/simple_factory_method/Main.java
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package learn.simple_factory_method;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Creator creator = new ConcreteCreator();
|
||||||
|
creator.anOperation();
|
||||||
|
}
|
||||||
|
}
|
4
src/learn/simple_factory_method/Product.java
Normal file
4
src/learn/simple_factory_method/Product.java
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
package learn.simple_factory_method;
|
||||||
|
|
||||||
|
public interface Product {
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user