task: Explanations and grammar fixes for all the GoF patterns (#1791)
* Grammatical fixes to command pattern * Update bridge pattern readme * Fixes to builder pattern grammar * Update chain of responsibility * Improvements to the composite example * Fixes to headings * Minor updates to decorator pattern * Update facade * Update factory example * Update factory method * Update flyweight * Interpreter explanation * Update iterator readme * Add explanation for mediator pattern * Grammatical fixes to memento * Grammar fixes for observer * Update explanation for the prototype pattern * Proxy pattern grammar fixes * Update singleton * Grammar fixes to state pattern * Grammar fixes for strategy * Grammar fixes, template method * Grammar fixes for visitor * Fix typo
This commit is contained in:
@ -30,8 +30,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
* 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.
|
||||
* <p>In this example an alchemist manufactures coins. CoinFactory is the factory class and it
|
||||
* provides a static method to create different types of coins.
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@ -41,9 +41,10 @@ public class App {
|
||||
* Program main entry point.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
var car1 = CarsFactory.getCar(CarType.FORD);
|
||||
var car2 = CarsFactory.getCar(CarType.FERRARI);
|
||||
LOGGER.info(car1.getDescription());
|
||||
LOGGER.info(car2.getDescription());
|
||||
LOGGER.info("The alchemist begins his work.");
|
||||
var coin1 = CoinFactory.getCoin(CoinType.COPPER);
|
||||
var coin2 = CoinFactory.getCoin(CoinType.GOLD);
|
||||
LOGGER.info(coin1.getDescription());
|
||||
LOGGER.info(coin2.getDescription());
|
||||
}
|
||||
}
|
||||
|
@ -24,9 +24,9 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Car interface.
|
||||
* Coin interface.
|
||||
*/
|
||||
public interface Car {
|
||||
public interface Coin {
|
||||
|
||||
String getDescription();
|
||||
|
@ -24,14 +24,14 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Factory of cars.
|
||||
* Factory of coins.
|
||||
*/
|
||||
public class CarsFactory {
|
||||
public class CoinFactory {
|
||||
|
||||
/**
|
||||
* Factory method takes as parameter a car type and initiate the appropriate class.
|
||||
* Factory method takes as a parameter the coin type and calls the appropriate class.
|
||||
*/
|
||||
public static Car getCar(CarType type) {
|
||||
public static Coin getCoin(CoinType type) {
|
||||
return type.getConstructor().get();
|
||||
}
|
||||
}
|
@ -28,15 +28,14 @@ import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
/**
|
||||
* Enumeration for different types of cars.
|
||||
* Enumeration for different types of coins.
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Getter
|
||||
public enum CarType {
|
||||
public enum CoinType {
|
||||
|
||||
FORD(Ford::new),
|
||||
FERRARI(Ferrari::new);
|
||||
|
||||
private final Supplier<Car> constructor;
|
||||
COPPER(CopperCoin::new),
|
||||
GOLD(GoldCoin::new);
|
||||
|
||||
private final Supplier<Coin> constructor;
|
||||
}
|
@ -24,11 +24,11 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Ferrari implementation.
|
||||
* CopperCoin implementation.
|
||||
*/
|
||||
public class Ferrari implements Car {
|
||||
public class CopperCoin implements Coin {
|
||||
|
||||
static final String DESCRIPTION = "This is Ferrari.";
|
||||
static final String DESCRIPTION = "This is a copper coin.";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
@ -24,11 +24,11 @@
|
||||
package com.iluwatar.factory;
|
||||
|
||||
/**
|
||||
* Ford implementation.
|
||||
* GoldCoin implementation.
|
||||
*/
|
||||
public class Ford implements Car {
|
||||
public class GoldCoin implements Coin {
|
||||
|
||||
static final String DESCRIPTION = "This is Ford.";
|
||||
static final String DESCRIPTION = "This is a gold coin.";
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
@ -27,12 +27,11 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class CarsFactoryTest {
|
||||
class CoinFactoryTest {
|
||||
|
||||
@Test
|
||||
void shouldReturnFerrariInstance() {
|
||||
final var ferrari = CarsFactory.getCar(CarType.FERRARI);
|
||||
assertTrue(ferrari instanceof Ferrari);
|
||||
void shouldReturnGoldCoinInstance() {
|
||||
final var goldCoin = CoinFactory.getCoin(CoinType.GOLD);
|
||||
assertTrue(goldCoin instanceof GoldCoin);
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user