mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.2 KiB
61 lines
1.2 KiB
3 months ago
|
## Factory Blueprint
|
||
|
|
||
|
### Instructions
|
||
|
|
||
|
You are given an incomplete Factory design pattern implementation with some incorrect parts. Complete and fix the class to demonstrate your understanding of how the Factory design pattern works.
|
||
|
|
||
|
### Expected Classes
|
||
|
|
||
|
```java
|
||
|
// Product interface
|
||
|
public interface Product {
|
||
|
void showDetails();
|
||
|
}
|
||
|
|
||
|
// ConcreteProductA class
|
||
|
public class ConcreteProductA {
|
||
|
...
|
||
|
}
|
||
|
|
||
|
// ConcreteProductB class
|
||
|
public class ConcreteProductB {
|
||
|
...
|
||
|
}
|
||
|
|
||
|
// Factory class
|
||
|
public class Factory {
|
||
|
public ConcreteProductB createProduct(String type) { // the type parametre accept two values `A` and `B`
|
||
|
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Usage
|
||
|
|
||
|
Here is a possible `ExerciseRunner.java` to test your classes:
|
||
|
|
||
|
```java
|
||
|
public class ExerciseRunner {
|
||
|
public static void main(String[] args) {
|
||
|
Factory factory = new Factory();
|
||
|
|
||
|
// Handle invalid product type
|
||
|
Object invalidProduct = factory.createProduct("C");
|
||
|
if (invalidProduct != null) {
|
||
|
invalidProduct.showDetails();
|
||
|
} else {
|
||
|
System.out.println("Invalid product type");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Expected Output
|
||
|
|
||
|
```shell
|
||
|
$ javac *.java -d build
|
||
|
$ java -cp build ExerciseRunner
|
||
|
Invalid product type
|
||
|
$
|
||
|
```
|