Skip to content

Commit b423fd3

Browse files
committed
Fix bugs, clean the code and add unit tests.
1 parent ac98b31 commit b423fd3

9 files changed

Lines changed: 148 additions & 51 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@startuml
2+
package com.iluwatar.simplefactory {
3+
class App {
4+
- LOGGER : Logger {static}
5+
+ App()
6+
+ main(args : String[]) {static}
7+
}
8+
interface Car {
9+
+ getDescription() : String {abstract}
10+
}
11+
class CarSimpleFactory {
12+
+ CarSimpleFactory()
13+
+ getCar(type : CarType) : Car {static}
14+
}
15+
~enum CarType {
16+
+ FERRARI {static}
17+
+ FORD {static}
18+
+ valueOf(name : String) : CarType {static}
19+
+ values() : CarType[] {static}
20+
}
21+
class Ferrari {
22+
~ DESCRIPTION : String {static}
23+
+ Ferrari()
24+
+ getDescription() : String
25+
}
26+
class Ford {
27+
~ DESCRIPTION : String {static}
28+
+ Ford()
29+
+ getDescription() : String
30+
}
31+
}
32+
CarType ..+ CarSimpleFactory
33+
Ferrari ..|> Car
34+
Ford ..|> Car
35+
@enduml

‎simple-factory/pom.xml‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
<version>1.24.0-SNAPSHOT</version>
77
</parent>
88
<artifactId>simple-factory</artifactId>
9+
<dependencies>
10+
<dependency>
11+
<groupId>org.junit.jupiter</groupId>
12+
<artifactId>junit-jupiter-engine</artifactId>
13+
<scope>test</scope>
14+
</dependency>
15+
<dependency>
16+
<groupId>junit</groupId>
17+
<artifactId>junit</artifactId>
18+
</dependency>
19+
</dependencies>
920
<build>
1021
<plugins>
1122
<!-- Maven assembly plugin is invoked with default setting which we have
@@ -18,7 +29,7 @@
1829
<configuration>
1930
<archive>
2031
<manifest>
21-
<mainClass>com.iluwatar.abstractfactory.App</mainClass>
32+
<mainClass>com.iluwatar.simplefactory.App</mainClass>
2233
</manifest>
2334
</archive>
2435
</configuration>
Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,42 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
124
package com.iluwatar.simplefactory;
225

326
import org.slf4j.Logger;
427
import org.slf4j.LoggerFactory;
528

629
public class App {
7-
8-
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
9-
10-
public static void main(String[] args) {
11-
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FORD);
12-
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FERRARI);
13-
LOGGER.info(car1.getDescription());
14-
LOGGER.info(car2.getDescription());
15-
}
30+
31+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
32+
33+
/**
34+
* Program main entry point.
35+
*/
36+
public static void main(String[] args) {
37+
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD);
38+
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
39+
LOGGER.info(car1.getDescription());
40+
LOGGER.info(car2.getDescription());
41+
}
1642
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.iluwatar.simplefactory;
22

33
/**
4-
* Car interface
4+
* Car interface.
55
*/
66
public interface Car {
7-
8-
public String getDescription();
9-
7+
8+
public String getDescription();
9+
1010
}
Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
package com.iluwatar.simplefactory;
22

3-
43
/**
5-
* Factory of cars
4+
* Factory of cars.
65
*/
76
public class CarSimpleFactory {
8-
9-
/*
10-
* Enumeration for different types of cars
11-
*/
12-
static enum carTypes {
13-
FORD, FERRARI
14-
};
15-
16-
/*
17-
* Factory method takes as parameter a car type and initiate the appropriate class
18-
*/
19-
public static Car getCar(carTypes type) {
20-
switch (type) {
21-
case FORD:
22-
return new Ford();
23-
case FERRARI:
24-
return new Ferrari();
25-
default:
26-
throw new IllegalArgumentException("Model not supported.");
27-
}
28-
}
7+
8+
/**
9+
* Enumeration for different types of cars.
10+
*/
11+
static enum CarType {
12+
FORD, FERRARI
13+
}
14+
15+
/**
16+
* Factory method takes as parameter a car type and initiate the appropriate class.
17+
*/
18+
public static Car getCar(CarType type) {
19+
switch (type) {
20+
case FORD: return new Ford();
21+
case FERRARI: return new Ferrari();
22+
default: throw new IllegalArgumentException("Model not supported.");
23+
}
24+
}
2925
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.iluwatar.simplefactory;
22

33
/**
4-
* Ferrari implementation
4+
* Ferrari implementation.
55
*/
66
public class Ferrari implements Car {
7-
8-
static final String DESCRIPTION = "This is Ferrari.";
9-
10-
@Override
11-
public String getDescription() {
12-
return DESCRIPTION;
13-
}
7+
8+
static final String DESCRIPTION = "This is Ferrari.";
9+
10+
@Override
11+
public String getDescription() {
12+
return DESCRIPTION;
13+
}
1414
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.iluwatar.simplefactory;
22

33
/**
4-
* Ford implementation
4+
* Ford implementation.
55
*/
66
public class Ford implements Car {
77

8-
static final String DESCRIPTION = "This is Ford.";
8+
static final String DESCRIPTION = "This is Ford.";
99

10-
@Override
11-
public String getDescription() {
12-
return DESCRIPTION;
13-
}
10+
@Override
11+
public String getDescription() {
12+
return DESCRIPTION;
13+
}
1414
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar.simplefactory;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class AppTest {
8+
9+
@Test
10+
void shouldExecuteWithoutExceptions() {
11+
assertDoesNotThrow(() -> App.main(new String[]{}));
12+
}
13+
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.simplefactory;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
class CarSimpleFactoryTest {
8+
9+
@Test
10+
void shouldReturnFerrariInstance() {
11+
final var ferrari = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
12+
assertTrue(ferrari instanceof Ferrari);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)