From 2879088ee7184cdc660c509ade59395634a65dad Mon Sep 17 00:00:00 2001 From: Arya <108480101+drafii@users.noreply.github.com> Date: Fri, 21 Oct 2022 22:04:41 +0300 Subject: [PATCH] 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. --- .../baeldung/factorygeneric/DateNotifier.java | 11 +++++ .../com/baeldung/factorygeneric/Main.java | 14 ++++++ .../com/baeldung/factorygeneric/Notifier.java | 6 +++ .../factorygeneric/NotifierFactory.java | 17 ++++++++ .../com/baeldung/factorygeneric/Record.java | 20 +++++++++ .../factorygeneric/StringNotifier.java | 9 ++++ .../FactoryGenericUnitTest.java | 43 +++++++++++++++++++ 7 files changed, 120 insertions(+) create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java create mode 100644 patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java create mode 100644 patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java new file mode 100644 index 0000000000..931fd29445 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/DateNotifier.java @@ -0,0 +1,11 @@ +package com.baeldung.factoryGeneric; + +import java.util.Date; + +public class DateNotifier implements Notifier { + + @Override + public void notify(Date date) { + System.out.println("Notifying: " + date); + } +} diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java new file mode 100644 index 0000000000..9ee570f4c4 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Main.java @@ -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 stringNotifier = factory.getNotifier(String.class); + Notifier dateNotifier = factory.getNotifier(Date.class); + + stringNotifier.notify("Hello world!"); + dateNotifier.notify(new Date()); + } +} diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java new file mode 100644 index 0000000000..8c7d9e4a56 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Notifier.java @@ -0,0 +1,6 @@ +package com.baeldung.factoryGeneric; + +public interface Notifier { + + void notify(T obj); +} diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java new file mode 100644 index 0000000000..67b980f198 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/NotifierFactory.java @@ -0,0 +1,17 @@ +package com.baeldung.factoryGeneric; + +import java.util.Date; + +public class NotifierFactory { + + public Notifier getNotifier(Class c) { + if (c == String.class) { + return Record.STRING.make(); + } + if (c == Date.class) { + return Record.DATE.make(); + } + return null; + } + +} diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java new file mode 100644 index 0000000000..0f99e1f988 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/Record.java @@ -0,0 +1,20 @@ +package com.baeldung.factoryGeneric; + +import java.util.Date; + +public enum Record { + STRING { + @Override + public Notifier make() { + return new StringNotifier(); + } + }, + DATE { + @Override + public Notifier make() { + return new DateNotifier(); + } + }; + + public abstract Notifier make(); +} diff --git a/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java new file mode 100644 index 0000000000..124bfa9a5a --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/main/java/com/baeldung/factorygeneric/StringNotifier.java @@ -0,0 +1,9 @@ +package com.baeldung.factoryGeneric; + +public class StringNotifier implements Notifier { + + @Override + public void notify(String str) { + System.out.println("Notifying: " + str); + } +} diff --git a/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java new file mode 100644 index 0000000000..4dd9880b74 --- /dev/null +++ b/patterns-modules/design-patterns-creational/src/test/java/com/baeldung/factorygeneric/FactoryGenericUnitTest.java @@ -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 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 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 stringNotifier = factory.getNotifier(String.class); + Notifier 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!"); + } + +}