Fix bugs, clean the code and add unit tests.

This commit is contained in:
Samil Ayoub 2020-09-02 18:12:42 +01:00
parent ac98b31b68
commit b423fd30d4
9 changed files with 148 additions and 51 deletions

View File

@ -0,0 +1,35 @@
@startuml
package com.iluwatar.simplefactory {
class App {
- LOGGER : Logger {static}
+ App()
+ main(args : String[]) {static}
}
interface Car {
+ getDescription() : String {abstract}
}
class CarSimpleFactory {
+ CarSimpleFactory()
+ getCar(type : CarType) : Car {static}
}
~enum CarType {
+ FERRARI {static}
+ FORD {static}
+ valueOf(name : String) : CarType {static}
+ values() : CarType[] {static}
}
class Ferrari {
~ DESCRIPTION : String {static}
+ Ferrari()
+ getDescription() : String
}
class Ford {
~ DESCRIPTION : String {static}
+ Ford()
+ getDescription() : String
}
}
CarType ..+ CarSimpleFactory
Ferrari ..|> Car
Ford ..|> Car
@enduml

View File

@ -6,6 +6,17 @@
<version>1.24.0-SNAPSHOT</version>
</parent>
<artifactId>simple-factory</artifactId>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Maven assembly plugin is invoked with default setting which we have
@ -18,7 +29,7 @@
<configuration>
<archive>
<manifest>
<mainClass>com.iluwatar.abstractfactory.App</mainClass>
<mainClass>com.iluwatar.simplefactory.App</mainClass>
</manifest>
</archive>
</configuration>

View File

@ -1,16 +1,42 @@
/*
* The MIT License
* Copyright © 2014-2019 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.simplefactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FORD);
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.carTypes.FERRARI);
LOGGER.info(car1.getDescription());
LOGGER.info(car2.getDescription());
}
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
/**
* Program main entry point.
*/
public static void main(String[] args) {
Car car1 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FORD);
Car car2 = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
LOGGER.info(car1.getDescription());
LOGGER.info(car2.getDescription());
}
}

View File

@ -1,10 +1,10 @@
package com.iluwatar.simplefactory;
/**
* Car interface
* Car interface.
*/
public interface Car {
public String getDescription();
public String getDescription();
}

View File

@ -1,29 +1,25 @@
package com.iluwatar.simplefactory;
/**
* Factory of cars
* Factory of cars.
*/
public class CarSimpleFactory {
/*
* Enumeration for different types of cars
*/
static enum carTypes {
FORD, FERRARI
};
/*
* Factory method takes as parameter a car type and initiate the appropriate class
*/
public static Car getCar(carTypes type) {
switch (type) {
case FORD:
return new Ford();
case FERRARI:
return new Ferrari();
default:
throw new IllegalArgumentException("Model not supported.");
}
}
/**
* Enumeration for different types of cars.
*/
static enum CarType {
FORD, FERRARI
}
/**
* Factory method takes as parameter a car type and initiate the appropriate class.
*/
public static Car getCar(CarType type) {
switch (type) {
case FORD: return new Ford();
case FERRARI: return new Ferrari();
default: throw new IllegalArgumentException("Model not supported.");
}
}
}

View File

@ -1,14 +1,14 @@
package com.iluwatar.simplefactory;
/**
* Ferrari implementation
* Ferrari implementation.
*/
public class Ferrari implements Car {
static final String DESCRIPTION = "This is Ferrari.";
@Override
public String getDescription() {
return DESCRIPTION;
}
static final String DESCRIPTION = "This is Ferrari.";
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -1,14 +1,14 @@
package com.iluwatar.simplefactory;
/**
* Ford implementation
* Ford implementation.
*/
public class Ford implements Car {
static final String DESCRIPTION = "This is Ford.";
static final String DESCRIPTION = "This is Ford.";
@Override
public String getDescription() {
return DESCRIPTION;
}
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -0,0 +1,14 @@
package com.iluwatar.simplefactory;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class AppTest {
@Test
void shouldExecuteWithoutExceptions() {
assertDoesNotThrow(() -> App.main(new String[]{}));
}
}

View File

@ -0,0 +1,15 @@
package com.iluwatar.simplefactory;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class CarSimpleFactoryTest {
@Test
void shouldReturnFerrariInstance() {
final var ferrari = CarSimpleFactory.getCar(CarSimpleFactory.CarType.FERRARI);
assertTrue(ferrari instanceof Ferrari);
}
}