Implementing Factory Pattern With Generics in Java (#12895)

* Implementing the factory pattern with generics.

* Did some modifications. Also added unit test.

* Changed the package name.
This commit is contained in:
Arya 2022-10-21 22:04:41 +03:00 committed by GitHub
parent ff3f356fcd
commit 2879088ee7
7 changed files with 120 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package com.baeldung.factoryGeneric;
import java.util.Date;
public class DateNotifier implements Notifier<Date> {
@Override
public void notify(Date date) {
System.out.println("Notifying: " + date);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.factoryGeneric;
import java.util.Date;
class Main {
public static void main(String[] args) {
NotifierFactory factory = new NotifierFactory();
Notifier<String> stringNotifier = factory.getNotifier(String.class);
Notifier<Date> dateNotifier = factory.getNotifier(Date.class);
stringNotifier.notify("Hello world!");
dateNotifier.notify(new Date());
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.factoryGeneric;
public interface Notifier<T> {
void notify(T obj);
}

View File

@ -0,0 +1,17 @@
package com.baeldung.factoryGeneric;
import java.util.Date;
public class NotifierFactory {
public <T> Notifier<T> getNotifier(Class<T> c) {
if (c == String.class) {
return Record.STRING.make();
}
if (c == Date.class) {
return Record.DATE.make();
}
return null;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.factoryGeneric;
import java.util.Date;
public enum Record {
STRING {
@Override
public Notifier<String> make() {
return new StringNotifier();
}
},
DATE {
@Override
public Notifier<Date> make() {
return new DateNotifier();
}
};
public abstract <T> Notifier<T> make();
}

View File

@ -0,0 +1,9 @@
package com.baeldung.factoryGeneric;
public class StringNotifier implements Notifier<String> {
@Override
public void notify(String str) {
System.out.println("Notifying: " + str);
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.factoryGeneric;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Date;
// Unit test for the NotifierFactory class.
public class FactoryGenericUnitTest {
// Checks that when the factory is asked to provide a string notifier, the object is an actual string notifier.
@Test
public void givenStringNotifier_whenEquals_thenTrue() {
NotifierFactory factory = new NotifierFactory();
Notifier<String> stringNotifier = factory.getNotifier(String.class);
assertNotNull(stringNotifier, "The object returned by the factory is null!");
assertTrue(stringNotifier instanceof Notifier<?>, "The object returned by the factory is not a notifier!");
}
// Checks that when the factory is asked to provide a date notifier, the object is an actual date notifier.
@Test
public void givenDateNotifier_whenEquals_thenTrue() {
NotifierFactory factory = new NotifierFactory();
Notifier<Date> dateNotifier = factory.getNotifier(Date.class);
assertNotNull(dateNotifier, "The object returned by the factory is null!");
assertTrue(dateNotifier instanceof Notifier<?>, "The object returned by the factory is not a notifier!");
}
// Checks that when the factory is asked to provide both a date notifier and a string notifier, the objects returned are different.
@Test
public void givenDateNotifierAndStringNotifier_whenEquals_thenFalse() {
NotifierFactory factory = new NotifierFactory();
Notifier<String> stringNotifier = factory.getNotifier(String.class);
Notifier<Date> dateNotifier = factory.getNotifier(Date.class);
assertNotNull(stringNotifier, "The object returned by the factory is null!");
assertNotNull(dateNotifier, "The object returned by the factory is null!");
assertNotEquals(stringNotifier, dateNotifier, "The string notifier and date notifier objects returned by the factory are actually the same!");
}
}