- README.md is added
- Change the name to factory is done - Local variable type inference is used
This commit is contained in:
51
factory/src/main/java/com/iluwatar/factory/App.java
Normal file
51
factory/src/main/java/com/iluwatar/factory/App.java
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.factory;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* Factory is an object for creating other objects, it providing Providing a static method to
|
||||
* create and return objects of varying classes, in order to hide the implementation logic
|
||||
* and makes client code focus on usage rather then objects initialization and management.
|
||||
*
|
||||
* <p>In this example the CarFactory is the factory class and it provides a static method to
|
||||
* create different cars.
|
||||
*/
|
||||
|
||||
public class App {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||
|
||||
/**
|
||||
* Program main entry point.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var car1 = CarsFactory.getCar(CarsFactory.CarType.FORD);
|
||||
var car2 = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
|
||||
LOGGER.info(car1.getDescription());
|
||||
LOGGER.info(car2.getDescription());
|
||||
}
|
||||
}
|
10
factory/src/main/java/com/iluwatar/factory/Car.java
Normal file
10
factory/src/main/java/com/iluwatar/factory/Car.java
Normal file
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Car interface.
|
||||
*/
|
||||
public interface Car {
|
||||
|
||||
public String getDescription();
|
||||
|
||||
}
|
25
factory/src/main/java/com/iluwatar/factory/CarsFactory.java
Normal file
25
factory/src/main/java/com/iluwatar/factory/CarsFactory.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Factory of cars.
|
||||
*/
|
||||
public class CarsFactory {
|
||||
|
||||
/**
|
||||
* 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.");
|
||||
}
|
||||
}
|
||||
}
|
14
factory/src/main/java/com/iluwatar/factory/Ferrari.java
Normal file
14
factory/src/main/java/com/iluwatar/factory/Ferrari.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Ferrari implementation.
|
||||
*/
|
||||
public class Ferrari implements Car {
|
||||
|
||||
static final String DESCRIPTION = "This is Ferrari.";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
14
factory/src/main/java/com/iluwatar/factory/Ford.java
Normal file
14
factory/src/main/java/com/iluwatar/factory/Ford.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Ford implementation.
|
||||
*/
|
||||
public class Ford implements Car {
|
||||
|
||||
static final String DESCRIPTION = "This is Ford.";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return DESCRIPTION;
|
||||
}
|
||||
}
|
14
factory/src/test/java/com/iluwatar/factory/AppTest.java
Normal file
14
factory/src/test/java/com/iluwatar/factory/AppTest.java
Normal file
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteWithoutExceptions() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CarsFactoryTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnFerrariInstance() {
|
||||
final var ferrari = CarsFactory.getCar(CarsFactory.CarType.FERRARI);
|
||||
assertTrue(ferrari instanceof Ferrari);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user