From 880bd891b99448731276d86390940bc73e69c1c9 Mon Sep 17 00:00:00 2001 From: Alejandro Gervasio Date: Thu, 16 Aug 2018 23:00:43 -0300 Subject: [PATCH] BAEL-2066 - Java Constructors vs Static Factory Methods (#4934) * Initial Commit * Update User.java * Update UserUnitTest.java * Update User.java - UserUnitTest.java --- .../application/Application.java | 12 ++++ .../entities/User.java | 63 +++++++++++++++++++ .../UserUnitTest.java | 43 +++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/application/Application.java create mode 100644 core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/entities/User.java create mode 100644 core-java/src/test/java/com/baeldung/constructorsstaticfactorymethods/UserUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/application/Application.java b/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/application/Application.java new file mode 100644 index 0000000000..d19772072f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/application/Application.java @@ -0,0 +1,12 @@ +package com.baeldung.constructorsstaticfactorymethods.application; + +import com.baeldung.constructorsstaticfactorymethods.entities.User; + +public class Application { + + public static void main(String[] args) { + User user1 = User.createWithDefaultCountry("John", "john@domain.com"); + User user2 = User.createWithLoggedInstantiationTime("John", "john@domain.com", "Argentina"); + User user3 = User.getSingletonInstance("John", "john@domain.com", "Argentina"); + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/entities/User.java b/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/entities/User.java new file mode 100644 index 0000000000..ec7bf6b5fa --- /dev/null +++ b/core-java/src/main/java/com/baeldung/constructorsstaticfactorymethods/entities/User.java @@ -0,0 +1,63 @@ +package com.baeldung.constructorsstaticfactorymethods.entities; + +import java.time.LocalTime; +import java.util.logging.ConsoleHandler; +import java.util.logging.Level; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; + +public class User { + + private static User instance = null; + private static final Logger LOGGER = Logger.getLogger(User.class.getName()); + private final String name; + private final String email; + private final String country; + + public static User createWithDefaultCountry(String name, String email) { + return new User(name, email, "Argentina"); + } + + public static User createWithLoggedInstantiationTime(String name, String email, String country) { + setLoggerProperties(); + LOGGER.log(Level.INFO, "Creating User instance at : {0}", LocalTime.now()); + return new User(name, email, country); + } + + public static User getSingletonInstance(String name, String email, String country) { + if (instance == null) { + synchronized (User.class) { + if (instance == null) { + instance = new User(name, email, country); + } + } + } + return instance; + + } + + private User(String name, String email, String country) { + this.name = name; + this.email = email; + this.country = country; + } + + public String getName() { + return name; + } + + public String getEmail() { + return email; + } + + public String getCountry() { + return country; + } + + private static void setLoggerProperties() { + ConsoleHandler handler = new ConsoleHandler(); + handler.setLevel(Level.INFO); + handler.setFormatter(new SimpleFormatter()); + LOGGER.addHandler(handler); + } +} diff --git a/core-java/src/test/java/com/baeldung/constructorsstaticfactorymethods/UserUnitTest.java b/core-java/src/test/java/com/baeldung/constructorsstaticfactorymethods/UserUnitTest.java new file mode 100644 index 0000000000..0c0266a111 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/constructorsstaticfactorymethods/UserUnitTest.java @@ -0,0 +1,43 @@ +package com.baeldung.constructorsstaticfactorymethods; + +import com.baeldung.constructorsstaticfactorymethods.entities.User; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class UserUnitTest { + + @Test + public void givenUserClass_whenCalledcreateWithDefaultCountry_thenCorrect() { + assertThat(User.createWithDefaultCountry("John", "john@domain.com")).isInstanceOf(User.class); + } + + @Test + public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetName_thenCorrect() { + User user = User.createWithDefaultCountry("John", "john@domain.com"); + assertThat(user.getName()).isEqualTo("John"); + } + + @Test + public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetEmail_thenCorrect() { + User user = User.createWithDefaultCountry("John", "john@domain.com"); + assertThat(user.getEmail()).isEqualTo("john@domain.com"); + } + + @Test + public void givenUserIntanceCreatedWithcreateWithDefaultCountry_whenCalledgetCountry_thenCorrect() { + User user = User.createWithDefaultCountry("John", "john@domain.com"); + assertThat(user.getCountry()).isEqualTo("Argentina"); + } + + @Test + public void givenUserInstanceCreatedWithcreateWithInstantiationTime_whenCalledcreateWithInstantiationTime_thenCorrect() { + assertThat(User.createWithLoggedInstantiationTime("John", "john@domain.com", "Argentina")).isInstanceOf(User.class); + } + + @Test + public void givenUserInstanceCreatedWithgetSingletonIntance_whenCalledgetSingletonInstance_thenCorrect() { + User user1 = User.getSingletonInstance("John", "john@domain.com", "Argentina"); + User user2 = User.getSingletonInstance("John", "john@domain.com", "Argentina"); + assertThat(user1).isEqualTo(user2); + } +} \ No newline at end of file