BAEL-2066 - Java Constructors vs Static Factory Methods (#4934)

* Initial Commit

* Update User.java

* Update UserUnitTest.java

* Update User.java - UserUnitTest.java
This commit is contained in:
Alejandro Gervasio 2018-08-16 23:00:43 -03:00 committed by KevinGilmore
parent fad21ead40
commit 880bd891b9
3 changed files with 118 additions and 0 deletions

View File

@ -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");
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}