Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public class AppStarter {

public static void startup(Profile profile) {
StarterFactory starter = switch (profile) {
case PROD -> new ProdStarterFactory();
case DEV -> new DevStarterFactory();
};

starter.getDatabaseConnection().connect();
starter.getLoggingService().init();
starter.getSecurityPolicy().applyPolicy();
}

public enum Profile {
PROD,
DEV;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public interface DatabaseConnection {
void connect();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public class DevStarterFactory implements StarterFactory {

@Override
public DatabaseConnection getDatabaseConnection() {
return () -> System.out.println("Connecting to in-memory H2 database 🗄️.");
}

@Override
public LoggingService getLoggingService() {
return () -> System.out.println("Using DEV 🟩 logging service.");
}

@Override
public SecurityPolicy getSecurityPolicy() {
return () -> System.out.println("🔓 Applying no security.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public interface LoggingService {
void init();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public class ProdStarterFactory implements StarterFactory {

@Override
public DatabaseConnection getDatabaseConnection() {
return () -> System.out.println("Connecting to PostgreSQL database 🗄️.");
}

@Override
public LoggingService getLoggingService() {
return () -> System.out.println("Using PROD 🟥 logging service.");
}

@Override
public SecurityPolicy getSecurityPolicy() {
return () -> System.out.println("🔒 Applying strict security policies.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public interface SecurityPolicy {
void applyPolicy();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

public interface StarterFactory {

DatabaseConnection getDatabaseConnection();

LoggingService getLoggingService();

SecurityPolicy getSecurityPolicy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public Animal getNewInstance(AnimalType type) {
}

public enum AnimalType {
DOG(() -> new Dog()),
CAT(() -> new Cat()),
FLY(() -> new Fly());
DOG(Dog::new),
CAT(Cat::new),
FLY(Fly::new);

private final Supplier<Animal> creator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class AnimalFactoryMapImpl implements AnimalFactory<String> {
private final Map<String, Supplier<Animal>> nameToCreator = new HashMap<>();

public AnimalFactoryMapImpl() {
nameToCreator.put(Fly.NAME, () -> new Fly());
nameToCreator.put(Dog.NAME, () -> new Dog());
nameToCreator.put(Cat.NAME, () -> new Cat());
nameToCreator.put(Fly.NAME, Fly::new);
nameToCreator.put(Dog.NAME, Dog::new);
nameToCreator.put(Cat.NAME, Cat::new);
}

public Animal getNewInstance(String type) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.mperor.lab.java.design.pattern.creational.factory.abs;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.common.TestUtils;

import java.util.stream.Stream;

public class AppStarterAbstractFactoryTest {

@Test
public void shouldAllowToCreateAppStarterForConcreteProfile() {
var out = TestUtils.setTempSystemOut();

AppStarter.startup(AppStarter.Profile.DEV);
Assertions.assertLinesMatch(
Stream.of(
"Connecting to in-memory H2 database 🗄️.",
"Using DEV 🟩 logging service.",
"🔓 Applying no security."
), out.all().lines()
);

AppStarter.startup(AppStarter.Profile.PROD);
Assertions.assertLinesMatch(
Stream.of(
"Connecting to PostgreSQL database 🗄️.",
"Using PROD 🟥 logging service.",
"🔒 Applying strict security policies."
), out.all().lines().skip(3)
);

TestUtils.resetSystemOut();
}

}
Loading