많은 클래스가 하나 이상의 자원에 의존한다.(컴포지션)

이 의존하는 클래스를 정적 유틸리티 클래스(아이템 4)로 구현하게 되면 유연하지 않고 테스트 하기 어려운 구조가 된다.

 

  • 정적 유틸리티의 안좋은 예- 유연하지 않고 테스트 어렵다.

public class SpellChecker {

  private final Lexicon dictionary = ...;

  private SpellChecker() { }
  public static SpellChecker INSTANCE = new SpellChecker();

  public static boolean isValid(String word) { ... }
  public static List<String> suggestions(String typo) { ... }
}

 

  • 싱글톤의 안좋은 예- 유연하지 않고 테스트 어렵다.
public class SpellChecker {

  private final Lexicon dictionary = ...;

  private SpellChecker() { }
  public static SpellChecker INSTANCE = new SpellChecker();

  public static boolean isValid(String word) { ... }
  public static List<String> suggestions(String typo) { ... }
}

 

  • SpellChecker가 여러 사전을 사용할 수 있도록 하기 위해서 좋지 않은 방법 - 멀티스레드 환경X
public class SpellChecker {

  private Lexicon dictionary = ...;

  public static void changeDictionary(){
		this.dictionary = ...
  }
}

 

클래스가 내부적으로 하나 이상의 자원에 의존하고, 그 자원이 클래스 동작에 영향을 준 다면 싱글턴과 정적 유틸리티 클래스는 사용하지 않는 것이 좋다. 

이 자원들을 클래스가 직접 만들게 해서도 안 된다. 

 

 

대신 인스턴스를 생성할 때 생성자에 필요한 자원을 넘겨주는 방식을 사용한다.(불변 보장)

이는 의존 객체 주입의 한 형태로, 객체를 생성할 때 의존 객체를 주입해주면 된다. => c.f. Strategy Pattern

public class SpellChecker {

  private final Lexicon dictionary;

  public SpellChecker(Lexicon dictionary) { // 인스턴스를 생성할 때 생성자에 필요한 자원을 넘겨준다
    this.dictionary = dictionary;
  }

  public static boolean isValid(String word) { ... }
  public static List<String> suggestions(String typo) { }

}

 

 

이 패턴의 변형으로, 생성자에 자원 팩터리를 넘겨주는 방식이 있다.(Factory Method Pattern)

팩터리란 호출할 때마다 특정 타입의 인스턴스를 반복해서 만들어주는 객체를 말한다.

 

ex) Supplier<T> interface(java8)

Supplier<T>를 입력으로 받는 메서드는 일반적으로 한정적 와일드카드 타입을 사용해 팩터리의 타입 매개변수를 제한해야 한다. 

이 방식을 사용해 클라이언트는 자신이 명시한 타입의 하위 타입이라면 무엇이든 생성할 수 있는 팩터리를 넘길 수 있다. 

// 클라이언트가 제공한 팩터리가 생성한 타일(Tile)들로 구성된 모자이크 (Mosaic)를 만드는 메서드
Mosaic create(Supplier<? extends Tile> tileFactory) { ... }

 

예를 들어 어떤 사람의 이름과 생일을 입력해두고 getAge()로 나이를 가져오는 Person 클래스를 만든다고 하자.

package com.example.sypark9646.item5;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class Person {

	final String name;
	private final LocalDate dateOfBirth;
	private final LocalDate currentDate;

	public Person(String name, LocalDate dateOfBirth) {
		this(name, dateOfBirth, LocalDate.now());
	}

	public Person(String name, LocalDate dateOfBirth, LocalDate currentDate) {
		this.name = name;
		this.dateOfBirth = dateOfBirth;
		this.currentDate = currentDate;
	}

	long getAge() {
		return ChronoUnit.YEARS.between(dateOfBirth, currentDate);
	}

	public static void printAge(PersonSupplierConstruct person) {
		System.out.println(person.name + " is " + person.getAge());
	}
}

위 방법의 경우, getAge ()는 현재 날짜가 아닌 Person 객체가 생성 된 시기를 기반으로 한다.

이 문제는 Supplier <LocalDate>를 사용하면 해결된다. 현재 시간을 Supplier를 이용하여 주입하는 것이다.

package com.example.sypark9646.item5;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.function.Supplier;

public class PersonSupplierConstruct {

	final String name;
	private final LocalDate dateOfBirth;
	private final Supplier<LocalDate> currentDate;

	public PersonSupplierConstruct(String name, LocalDate dateOfBirth) {
		this(name, dateOfBirth, LocalDate::now);
	}

	public PersonSupplierConstruct(String name, LocalDate dateOfBirth, Supplier<LocalDate> currentDate) {
		this.name = name;
		this.dateOfBirth = dateOfBirth;
		this.currentDate = currentDate;
	}

	public long getAge() {
		return ChronoUnit.YEARS.between(dateOfBirth, currentDate.get());
	}

	public static void printAge(PersonSupplierConstruct person) {
		System.out.println(person.name + " is " + person.getAge());
	}
}

 

 

용어

  • 한정적 와일드카드 타입(bounded wildcard type, 아이템 31)
item3 -
싱글턴을 만드는 방식
기본적으로 생성자는 private 으로 감춰두고, => 인스턴스 생성 불가, 서브클래스 생성 불가
유일한 인스턴스에 접근할 수 있는 수단으로 public static 멤버 를 하나 마련해둔다.

 

정적 메서드와 정적 필드만을 담은 클래스를 만드는 경우

  • 기본 타입 값이나 배열 관련 메서드들을 모아놓는 경우: java.lang.Math, java.util.Arrays
  • 특정 인터페이스를 구현하는 객체를 생성해주는 정적 메서드/팩터리 메소드들을 모아놓는 경우: java.util.Collections 
  • final 클래스와 관 련한 메서드들을 모아놓는 경우(final 클래스를 상속해서 하위 클래스에 메서드를 넣는 건 불가능)

생성자를 명시하지 않으면 컴파일러가 자동으로 기본 생성자(매개변수 없는 public 기본 생성자)를 만들어준다.

그런데 읽다가 이런 이야기가 있었다.

추상 클래스로 만드는 것으로는 인스턴스화를 막을 수 없다.
아래와 같이 하위 클래스를 만들어 인스턴스화하면 그만이다.
이를 본 사용자는 상속해서 쓰라는 뜻으로 오해할 수 있으니 더 큰 문제다. (아이템 19)

 

무슨 말일까... 자바 공식 문서를 한번 보자

docs.oracle.com/javase/tutorial/java/IandI/abstract.html

 

Abstract Methods and Classes (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritanc

The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated

docs.oracle.com

 Abstract classes cannot be instantiated, but they can be subclassed.

여기서 나온 예제를 직접 한 번 구현해 보았다.

 

Shape는 추상 클래스로 두고, Circle과 Rectangle은 이를 extends 해서 구체화 하여 구현할 수 있다.

package com.example.sypark9646.item4;

public abstract class Shape {

	protected int x, y;

	public Shape() {
		System.out.println("Shape 호출");
	}

	public Shape(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public abstract String getName();

	public void drawCenter() {
		System.out.println("x = " + x + ", y = " + y);
	}
}
package com.example.sypark9646.item4;

public class Circle extends Shape {

	int radius;

	public Circle() {
		// super(); 묵시적 호출
		System.out.println("Circle 호출");
	}

	public Circle(int x, int y, int radius) {
		super(x, y);
		this.radius = radius;
	}

	@Override
	public void drawCenter() {
		super.drawCenter();
		System.out.println("radius = " + radius);
	}

	@Override
	public String getName() {
		return "circle" + this.hashCode();
	}
}
package com.example.sypark9646.item4;

public class Rectangle extends Shape {

	int row, col;

	public Rectangle() {
		// super(); 묵시적 호출
		System.out.println("Rectangle 호출");
	}

	public Rectangle(int x, int y, int row, int col) {
		super(x, y);
		this.row = row;
		this.col = col;
	}

	@Override
	public void drawCenter() {
		super.drawCenter();
		System.out.println("row = " + row + ", col = " + col);
	}

	@Override
	public String getName() {
		return "rectangle" + this.hashCode();
	}
}

 

추상클래스의 경우 아래와 같이 그냥 Shape 그 자체를 인스턴스화 할 순 없지만,

Circle과 Rectangle은 생성자를 통해 인스턴스화가 가능하고, 이 생성자들을 부르게 되면 상위 클래스 Shape의 생성자를 호출하게 된다.

package com.example.sypark9646.item4;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class AbstractInstantiateTest {

	@Test
	@DisplayName("추상클래스 인스턴스화 테스트")
	public void testInstantiateShape() throws InterruptedException {
		Shape circle = new Circle();
		Shape rectangle = new Rectangle();

		circle.drawCenter();
		rectangle.drawCenter();
	}
}

위와 같이 Circle, Rectangle 인스턴스를 생성하기 위해서는 반드시 생성자를 호출해야 한다.

하지만 생성자는 상속되지 않고 멤버만 상속된다. 대신 자식 클래스로 인스턴스를 생성하게 되면 부모 클래스의 생성자를 super로 호출한다.

즉, 부모 클래스의 생성자가 호출되어야 자식 클래스를 인스턴스화할 수 있다.

 

 

 

그래서 어떤 클래스의 인스턴스화를 확실히 막는 방법은 private 기본 생성자를 추가하는 것이다.

위에서 제시한 java.lang.Math, java.util.Arrays, 그리고 java.util.Collections 또한 private 생성자로 인스턴스화를 막고 있는 것을 확인할 수 있었다.

public class Collections {
    // Suppresses default constructor, ensuring non-instantiability.
    private Collections() {}
}

public final class Math {

    /**
     * Don't let anyone instantiate this class.
     */
    private Math() {}
}

 

만약 유틸리티 클래스를 만들게 된다면 아래와 같이 만들면 된다.

public class Utilityclass {
    // 기본 생성자가 만들어지는 것을 막는다, 인스턴스화 방지용
    private Utilityclass() { throw new AssertionError(); }
}

생성자를 명시적으로 private이니 클래스 바깥에서는 접근하지 못하도록 하고,

꼭 Assertion Error를 던질 필요는 없지만, 클래스 안에서 실수로라도 생성자를 호출하지 않도록 해준다.

즉, 어떤 환경에서도 클래스가 인스턴스화되는 것을 막아 준다.

추가적으로 사용자가 이해하기 쉽도록 생성자가 존재하는데 호출할 수 없다는 내용을 주석으로 달아주면 더 좋다.

이 방식은 상속을 불가능하게 하는 효과도 있다. 

Shape 예제에서 볼 수 있듯이 모든 생성자는 명시적이든 묵시적이든 부모 클래스의 생성자를 호출하게 되는데, 

이를 private으로 선언 하게 되면 하위 클래스가 상위 클래스의 생성자에 접근할 길이 막혀버리기 때문에 컴파일 타임에 상속이 불가능하다는 것을 알 수 있다.

 

 

Abstract class vs Interface

인스턴스화를 막는 방법으로서 private 기본 생성자를 추가하는 예시를 자바 컬렉션에서 찾아보다가 궁금한 점이 생겼다

HashSet의 경우 AbstractSet 추상클래스를 상속하고 있는데, Set 인터페이스또한 implement하고 있다.

그런데 AbstractSet은 Set을 implement하고 있다.

그렇다면 HashSet에서는 Set을 이미 implement하고 있는 AbstractSet만 상속하여 구현하면 될텐데 왜 굳이 두가지를 모두 상속하고 구현한 것일까?

 

public class HashSet<E> extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable{...}

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {...}

public interface Set<E> extends Collection<E> {...}

 

 

Abstract class가 Interface를 implement하는 이유는 무엇이고 어떻게 동작하는 것일까?

이와 관련된 질문을 스택오버플로우에서 찾을 수 있었다.

출처: https://stackoverflow.com/questions/49757423/what-happens-when-an-abstract-class-implements-an-interface-in-java
What exactly happens when an abstract class implements an interface? Does this work like inheritance, i.e. all interface methods belong also to the abtract class eventhough they are not implemented in it? Or will only the implemented methods belong to the abstract class? So is there any difference between implements and extends, except that one is used to implement interfaces and the other one is used for inheritance?

이에 대한 답변은 정리하자면 아래와 같다.

if you have an abstract class and implement an interface with it, you have two options for the interface methods.

예를 들어 소리를 낼 수 있는지 여부를 나타내는 인터페이스 CanMakeNoise, 동물을 나타내는 추상클래스 Animal이 있다고 하자

package com.example.sypark9646.item4;

public interface CanMakeNoise {

	void makeNoise();
}

 

1. implement them in the abstract class

package com.example.sypark9646.item4;

public abstract class Animal implements CanMakeNoise {

	public abstract void jump();

	@Override
	public void makeNoise() { // interface 함수 구현
		System.out.println("animal noise");
	}
}

abstract class에 interface에서 정의한 함수를 구현하면 구체클래스에서는 이를 재 정의할 필요 없다

package com.example.sypark9646.item4;

public class Dog extends Animal implements CanMakeNoise{

	@Override
	public void jump() {
		System.out.println("dog jumps");
	}

//	@Override
//	public void makeNoise() {
//		System.out.println("dog noise");
//	}
}

 

물론 makeNoise 함수를 Override하여 구현 해줘도 되긴 하다. 이럴 경우 메소드가 오버라이드 되어서 "dog noise"가 나온다.

 

2. you leave them abstract, but then some of your more concrete children need to implement it.

만약 abstract class에서 interface 함수를 구현하지 않고 implement만 한다면

package com.example.sypark9646.item4;

public abstract class Animal implements CanMakeNoise {

	public abstract void jump();
}

구체 클래스 Dog에서는 두 abstract 함수를 필수적으로 모두 구현해 주어야 한다.

package com.example.sypark9646.item4;

public class Dog extends Animal implements CanMakeNoise {

	@Override
	public void jump() {
		System.out.println("dog jumps");
	}

	@Override
	public void makeNoise() {
		System.out.println("dog noise");
	}
}

 

이 예제의 경우는 Animal은 특정 동물이 어떻게 makeNoise 하는지 알 수 없기 때문에 함수의 구현을 구체 클래스로 남겨 두는 것이 좋다.(2번 방법)

 

 

 

이와 반대로, 인터페이스의 중복된 구현을 추상클래스로 빼서 중복을 방지하는 디자인 패턴이 있을 수 있다.(1번 방법)

 

출처: effectiveprogramming.tistory.com/entry/interface-abstract-class-concrete-class-%ED%8C%A8%ED%84%B4
 

interface -abstract class - concrete class 패턴(인터페이스 구현 중복 해결 패턴)

interface - abstract class - concrete class 패턴은 인터페이스 구현 시 자주 발생하게 되는 중복 구현을 방지하는 패턴이다. 해결하고자 하는 문제 - 구현해야 할 클래스에 대한 인터페이스가 이미 정해진

effectiveprogramming.tistory.com

 

 

딴길로 좀 샌거같은데..

자바 컬렉션에서는 Abstract class가 Interface를 implement하는 이유는 정리해 보자면

클래스가 실제로 해당 인터페이스를 구현한다는 것을 기억하기 위해서라고 한다. 즉, 주어진 클래스의 전체 계층 구조를 거치지 않고 코드를 이해하는 데 도움이 될 수 있으며 문서화 할 때 가독성이 좋기 때문이다.

 

또한 이펙티브 자바에서는 인터페이스와 함께 사용할 abstract skeletal 구현 클래스를 추가하여 인터페이스와 추상 클래스의 장점을 결합 할 수 있다고 했다.

인터페이스는 유형을 정의하여 기본 메서드를 제공하는 반면, skeletal 클래스는 기본 인터페이스 메서드 위에 남아있는 기본이 아닌 인터페이스 메서드를 구현한다.

skeletal 구현을 확장하면 인터페이스 구현에서 대부분의 작업이 필요한데, 이것이 템플릿 방법 패턴이다.

관례에 따라 skeletal 구현 클래스는 AbstractInterface라고 하고, 여기서 Interface는 구현하는 인터페이스의 이름이다.

예로는 아래의 추상 클래스들이 있다.

AbstractCollection
AbstractSet
AbstractList
AbstractMap

 

 

인터페이스를 명시적으로 구현하는 것과 상속으로 구현하는 것은 분명 다르긴 하다.

extends AbstractSet, implements Set이라고 되어있지만, 아래와 같이 리플렉션을 통해 보면...

소스에 작성된 순서대로 HashSet에 의해 명시적으로 구현 된 인터페이스만 표시한다는 것을 알 수 있다.

 

for (Class<?> c : ArrayList.class.getInterfaces())
    System.out.println(c);

// interface java.util.List
// interface java.util.RandomAccess
// interface java.lang.Cloneable
// interface java.io.Serializable

ArrayList 또한 마찬가지이다. 

출력에는 super class에 의해 구현 된 인터페이스 또는 포함 된 super interface인 인터페이스가 포함되지 않는다.

public interface List<E> extends Collection<E> {
	...
}
public interface Collection<E> extends Iterable<E> {
	...
}

특히, ArrayList가 암시적으로 구현하더라도 Iterable과 Collection은 위에서 누락되었다는 것을 알 수 있다.

Collection과 Iterable을 찾으려면 클래스 계층 구조를 재귀 적으로 반복해야한다.

그렇지만 다행히 이 차이는 `new ArrayList <> () instanceof Iterable` 및 `Iterable.class.isAssignableFrom (ArrayList.class)`은 올바르게 ​​true로 나온다.

 

 

마지막으로 추상 클래스와 인터페이스의 특징에 대해 각각 알아보자면,

  • 추상클래스가 인터페이스보다 빠르다 (그렇지만 별 차이 없는 정도이다)
  • 인터페이스는 다중 상속이 가능하지만, 추상클래스는 최대 1개만 가능하다.
  • 추상클래스는 모든 추상메소드를 재구현(Override)해야하지만, 인터페이스는 필요한 것만 구현해도 된다.
  • 인터페이스에는 접근 제어자가 없다. 인터페이스 내부에서 선언된 모든 것들은 public만 가능하다. 반면 추상클래스는 접근제어자가 가능하다.
  • 인터페이스의 경우 다양한 하위 구현 클래스들이 같은 메소드 특징을 공유할 때 사용한다. 추상클래스는 주로 동일한 종류의 다양한 구현이 공통 동작을 공유 할 때 사용한다.
  • 인터페이스는 데이터 필드를 가질 수 없지만 추상 클래스는 가질 수 있다.
  • 인터페이스는 생성자가 없으며, 추상클래스는 생성자가 있다.
  • 인터페이스의 경우 java8부터 default 메소드를 통해 메소드 내부 구현이 가능하다.

 

싱글턴(singleton): 인스턴스를 오직 하나만 생성할 수 있는 클래스, 2021/01/19 - [책읽기/Design Patterns] - Singleton Pattern

 

클래스를 싱글턴으로 만들면 이를 사용하는 클라이언트를 테스트하기가 어려워질 수 있다.

타입을 인터페이스로 정의한 다음 그 인터페이스를 구현해서 만든 싱글턴이 아니라면, 싱글턴 인스턴스를 가짜(mock) 구현으로 대체할 수 없기 때문이다.

 

싱글턴을 만드는 방식

기본적으로 생성자는 private 으로 감춰두고, => 인스턴스 생성 불가, 서브클래스 생성 불가

유일한 인스턴스에 접근할 수 있는 수단으로 public static 멤버 를 하나 마련해둔다.

 

 

1. Eager Initialization

가장 간단한 형태의 구현 방법으로, 인스턴스를 클래스 로딩 단계에서 객체를 프로그램 시작과 동시에 초기화하는 방법이다.

package com.example.demo.item03;

public class EagerInitializationSingleton {

	private static final EagerInitializationSingleton instance = new EagerInitializationSingleton();

	private EagerInitializationSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static final EagerInitializationSingleton getInstance() {
		return instance;
	}
}

private 생성자는 private static final 필드인 instance를 초기화할 때 딱 한 번만 호출되고, getInstance는 항상 같은 객체의 참조를 반환하므로, EagerInitializationSingleton 클래스가 초기화될 때 만들어진 인스턴스가 전체 시스템에서 하나뿐임이 보장된다.

기본 생성자에서 예외를 리턴하는데, 클라이언트가 자바 리플렉션 API(아이템 65)를 사용하여 private 생성자를 호출할 수도 있다.

이러한 공격을 방어하려면 생성자를 수정하여 두 번째 객체가 생성되려 할 때 예외를 던지게 하면 된다.

package com.example.demo.item03;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SingletonCreateSecondExceptionTest {

	@Test
	public void testEagerInitializationSingletonException() throws IllegalAccessException, InvocationTargetException, InstantiationException {
		Constructor<?>[] constructors = EagerInitializationSingleton.class.getDeclaredConstructors();
		Constructor theConstructor = constructors[0];
		theConstructor.setAccessible(true);
		EagerInitializationSingleton singleton1 = (EagerInitializationSingleton) theConstructor.newInstance();
		EagerInitializationSingleton singleton2 = (EagerInitializationSingleton) theConstructor.newInstance();

		System.out.println(singleton1);
		System.out.println(singleton2);

		Assertions.assertSame(singleton1, singleton2);
	}
}

 

이 방법의 장점은 API를 바꾸지 않고도 싱글턴이 아니게 변경할 수 있다는 점이다. 또한, 두 번째 장점은 원한다면 정적 팩터리를 제네릭 싱글턴 팩터리로 만들 수 있다는 점이다(아이템 30). 세 번째 장점은 정적 팩터리의 메서드 참조를 공급자(supplier)로 사용할 수 있다는 점이다. 가령 Singleton::get Instance를 Supplier<Singleton>로 사용하는 식이다(아이템 43, 44). 

 

자세한 클래스 로딩 시점은 2020/12/18 - [작성중...] - JVM의 구조와 array, hashmap max size에 대한 생각 - 작성중...에서 볼 수 있다.

인스턴스를 사용하지 않더라도 인스턴스를 생성하기 때문에 낭비가 될 수 있다.

즉, File System, Database Connection 등 객체 생성에 많은 리소스를 사용하는 싱글톤을 구현할 때는 직접 사용할 때까지 싱글톤 인스턴스를 생성하지 않는 방법이 더 좋다.

//예시 java.util.HashSet
public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    // Dummy value to associate with an Object in the backing Map
    private static final Object PRESENT = new Object();

 ...
 }

 

2. Static Block Initialization

Eager Initialization과 유사하지만 static block을 통해서 Exception Handling을 제공한다.

Eager Initialization과 마찬가지로 클래스 로딩 단계에서 인스턴스를 생성하므로, 큰 리소스를 다루는 경우에는 적절하지 않다.

package com.example.demo.item03;

public class StaticBlockInitializationSingleton {

	private static StaticBlockInitializationSingleton instance;

	private StaticBlockInitializationSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	static {
		try {
			instance = new StaticBlockInitializationSingleton();
		} catch (Exception e) {
			throw new RuntimeException("Creating of this object is not allowed.");
		}
	}

	public static StaticBlockInitializationSingleton getInstance() {
		return instance;
	}
}

 

3. Lazy Initialization > single thread 에서만 사용

앞선 두 방식과는 달리 나중에 초기화하는 방법이다.

public 메소드 getInstance()를 호출할 때 인스턴스가 없는지 확인하고, 없다면 생성한다. 따라서 인스턴스 낭비를 막을 수 있다.

package com.example.demo.item03;

public class LazyInitializationSingleton {

	private static LazyInitializationSingleton instance;

	private LazyInitializationSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static LazyInitializationSingleton getInstance() {
		if (instance == null) {
			instance = new LazyInitializationSingleton();
		}
		return instance;
	}
}

하지만 multi-thread 환경에서 instance==null인 시점일 때 여러 쓰레드가 동시에 getInstance() 를 호출 한다면

thread-safe 하지 않을 수 있다는 치명적인 단점이 있다.

	@Test
	public void testLazyInitializationSingleton() throws InterruptedException {
		int numberOfThreads = 500;
		ExecutorService service = Executors.newFixedThreadPool(500);
		CountDownLatch latch = new CountDownLatch(numberOfThreads);

		HashSet<LazyInitializationSingleton> singletonHashSet = new HashSet<>();

		for (int i = 0; i < numberOfThreads; i++) {
			service.execute(() -> {
				LazyInitializationSingleton lazyInitializationSingleton = LazyInitializationSingleton.getInstance();
				singletonHashSet.add(lazyInitializationSingleton);
				latch.countDown();
			});
		}
		Assertions.assertEquals(singletonHashSet.size(), 1);
		latch.await();
	}

lazy initialization singleton 방식만 multi-thread 테스트를 통과하지 못함

 

4. Thread Safe Singleton

Lazy Initialization의 thread-safe 문제를 해결하기 위한 방법으로, getInstance() 메소드에 synchronized를 걸어두는 방식이다.

synchronized 키워드는 임계 영역(Critical Section)을 형성해 해당 영역에 오직 하나의 쓰레드만 접근 가능하게 해 준다.

package com.example.demo.item03;

public class SynchronizedSingleton {

	private static SynchronizedSingleton instance;

	private SynchronizedSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static synchronized SynchronizedSingleton getInstance() {
		if (instance == null) {
			instance = new SynchronizedSingleton();
		}
		return instance;
	}
}

getInstance() 메소드 내에 진입하는 쓰레드가 하나로 보장받기 때문에 멀티 쓰레드 환경에서도 정상 동작하게 된다.

그러나 synchronized 키워드 자체에 대한 비용이 크기 때문에 싱글톤 인스턴스 호출이 잦은 경우 성능이 떨어지게 됩니다.

 

5. Double checked Locking Singleton

synchronized 키워드가 성능상으로 좋지 않기 때문에,

getInstance() 메소드 수준에 lock을 걸지 않고 instance가 null일 경우에만 synchronized가 동작하도록 한다.

package com.example.demo.item03;

public class DoubleCheckingLockingSingleton {

	private static DoubleCheckingLockingSingleton instance;

	private DoubleCheckingLockingSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static DoubleCheckingLockingSingleton getInstance() {
		if (instance == null) {
			synchronized (DoubleCheckingLockingSingleton.class) {
				if (instance == null) {
					instance = new DoubleCheckingLockingSingleton();
				}
			}
		}
		return instance;
	}

}

 

6. Bill Pugh Singleton Implementation

Bill Pugh가 고안한 방식으로, inner static helper class를 사용하는 방식이다.

앞선 방식이 안고 있는 문제점들을 대부분 해결한 방식으로, 현재 가장 널리 쓰이는 싱글톤 구현 방법이다.

package com.example.demo.item03;

public class InitializationOnDemandHolderIdiomSingleton {

	private InitializationOnDemandHolderIdiomSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static InitializationOnDemandHolderIdiomSingleton getInstance() {
		return SingletonLazyHolder.INSTANCE;
	}

	private static class SingletonLazyHolder {

		private static final InitializationOnDemandHolderIdiomSingleton INSTANCE = new InitializationOnDemandHolderIdiomSingleton();
	}
}

private inner static class를 두어 싱글톤 인스턴스를 갖게 하는데,

inner class인 SingletonHelper 클래스는 Singleton 클래스가 로드 될 때가 아닌, getInstance()가 호출됐을 때

비로소 JVM 메모리에 로드되고, 인스턴스를 생성하게 된다.

(+ synchronized를 사용하지 않기 때문에 성능 문제가 해결된다.)

 

7. Enum 방식

앞선 1~6 방식은 자바 리플렉션 api를 이용하여 싱글톤을 파괴할 수 있다.

package com.example.demo.item03;

public enum EnumSingleton {
	INSTANCE;
}

enum 방식은 이와 달리 간결할 뿐만 아니라, 쉽게 직렬화할 수 있고, 제2의 인스턴스를 생성하는 리플렉션 공격도 완벽히 막아준다.

조금 부자연스러워 보일 수는 있으나 대부분 상황에서는 원소가 하나뿐인 열거 타입이 싱글턴을 만드는 가장 좋은 방법 일 수 있다.

단, 만들려는 싱글턴이 Enum 외의 클래스를 상속해야 한다면 이 방법은 사용할 수 없다(열거 타입이 다른 인터페이스를 구현하도록 선언할 수는 있다).

그러나 이 방법 또한 1, 2번과 같이 클래스를 사용하지 않을 경우 메모리 낭비를 한다는 단점이 있다.

 

싱글턴 클래스의 직렬화

싱글턴 클래스를 직렬화하려면(12장) 단순히 Serializable을 구현한다고 선언하는 것만으로는 부족하다.

package com.example.demo.item03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SingletonSerializeTests {

	@Test
	public void testEagerInitializationSingleton() throws IOException, ClassNotFoundException {
		EagerInitializationSingleton singleton1 = EagerInitializationSingleton.getInstance();
		ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("filename1.ser"));
		obs.writeObject(singleton1);
		obs.close();

		ObjectInputStream objInputStream = new ObjectInputStream(new FileInputStream("filename1.ser"));
		EagerInitializationSingleton singleton2 = (EagerInitializationSingleton) objInputStream.readObject();
		objInputStream.close();

		Assertions.assertSame(singleton1.getClass(), singleton2.getClass()); // true
		Assertions.assertSame(singleton1, singleton2); // false
		Assertions.assertSame(singleton1.hashCode(), singleton2.hashCode()); // false
	}
}

모든 인스턴스 필드를 일시적 (transient)이라고 선언하고 readResolve 메서드를 제공해야 한다(아이템 89).

이렇게 하지 않으면 직렬화된 인스턴스를 역직렬화할 때 마다 새로운 인스턴스가 만들어진다. 가짜 Singleton 클래스 생성을 예방하고 싶다면 싱글톤 클래스에 다음의 readResolve 메서드를 추가해야 한다.

package com.example.demo.item03;

import java.io.Serializable;

public class EagerInitializationSingleton implements Serializable {

	private static EagerInitializationSingleton instance = new EagerInitializationSingleton();

	private EagerInitializationSingleton() {
		if (instance != null) {
			throw new InstantiationError("Creating of this object is not allowed.");
		}
	}

	public static EagerInitializationSingleton getInstance() {
		return instance;
	}

	private Object readResolve() {
		return instance;
	}
}
package com.example.demo.item03;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class SingletonSerializeTests {

	@Test
	public void testEagerInitializationSingleton() throws IOException, ClassNotFoundException {
		EagerInitializationSingleton singleton1 = EagerInitializationSingleton.getInstance();
		ObjectOutputStream obs = new ObjectOutputStream(new FileOutputStream("filename1.ser"));
		obs.writeObject(singleton1);
		obs.close();

		ObjectInputStream objInputStream = new ObjectInputStream(new FileInputStream("filename1.ser"));
		EagerInitializationSingleton singleton2 = (EagerInitializationSingleton) objInputStream.readObject();
		objInputStream.close();

		Assertions.assertSame(singleton1.getClass(), singleton2.getClass()); // true
		Assertions.assertSame(singleton1, singleton2); // true
		Assertions.assertSame(singleton1.hashCode(), singleton2.hashCode()); // false
	}
}

같은 객체이기 때문에 hashcode 또한 같은 값을 리턴해야 한다고 생각했는데 다른 값을 리턴하는 것을 확인하고 이슈에 질문을 올렸다.

github.com/dolly0920/Effective_Java_Study/issues/9

 

[item 3] hashcode · Issue #9 · dolly0920/Effective_Java_Study

singleton 객체에 readResolve 함수를 구현하고 serialize & deserialize 해 보았는데 Assertions.assertSame(singleton1, singleton2)가 true를 리턴하는 것을 보아 같은 인스턴스로 직렬화 & 역직렬화가 된 것 같긴 한데 Ass

github.com

알고보니 Assertions.assertSame의 특성을 잘 이해하지 못하고 사용해서 문제가 생겼던 것이다.

assertSame 는 객체가 같은지 비교하는 가정문이다.

Assert.assertSame(1000, 1000); // false
Assert.assertSame(Integer.valueOf(1000), Integer.valueOf(1000)); // false
Assert.assertSame(new Integer(1000), new Integer(1000)); // false

assertSame 은 참조형(Reference Type)인 경우에만 사용해야 한다.
만약 기본형(Primitive Type)을 비교해야 한다면 값을 비교하는 가정문 assertEquals 을 사용해야 한다.

assertSame(Object arg, Object arg2) 의 파라미터 형은 Object 이기 때문에,
autoboxing이 일어나서 새로운 객체가 생성되어 같은 값이라도 hashCode 가 다르다 -> false 리턴

추가로,
Assert.assertSame(127, 127); // true
자바에서 -128 ~ 127 사이의 값은 미리 저장된 값을 이용하기 때문에 (새로운 객체를 생성하지 않아서) true를 리턴한다

 

용어

  • 무상태 (stateless) 객체: 
    •  인스턴스 변수가 없는 객체
    • Stateless object is an instance of a class without instance fields (instance variables). The class may have fields, but they are compile-time constants (static final).
    • A very much related term is immutable. Immutable objects may have state, but it does not change when a method is invoked (method invocations do not assign new values to fields). These objects are also thread-safe.
    • 장점: 다른 클래스에 종속적이지 않음, thread-safe, 인터페이스화 하기 용이하다
    • 공부해 볼 것: 
  • supplier: 인자는 받지않으며 리턴타입만 존재하는 메서드를 갖고있다. 순수함수에서 결과를 바꾸는건 오직 input parameter 뿐이다. 그런데 input이 없다는건 내부에서 랜덤함수같은것을 쓰는게 아닌이상 항상 같은 것을 리턴하는 메서드라는걸 알 수 있다.
  • synchronized: 여러개의 스레드가 한개의 자원을 사용하고자  , 현재 데이터를 사용하고 있는 해당 스레드를 제외하고 나머지 스레드들은 데이터에 접근할 수 없도록 막는다.
  • 임계영역: 둘 이상의 스레드가 동시에 실행될 경우 생길 수 있는 동시 접근 문제를 발생시킬 수 있는 코드 블록

정적 팩터리 메소드와 생성자는 선택적 매개변수가 많을 떄 적절하게 대응하기 어렵다는 단점이 있다.

만약 있을 수도 없을 수도 있는 필드가 많은 객체가 있다고 가정하자.

 

1. 옛날에는 이러한 클래스에서 점층적 생성자 패턴(telescoping constructor pattern)을 즐겨 사용했다.

이 패턴은 필수 매개변수를 받는 생성자와, 선택 매개변수를 하나씩 늘여가며 생성자를 만드는 패턴이다.

=> 매개변수 개수가 많아지면 클라이언트 코드를 작성하거나 읽기 어렵다

class Person {

	private String name; // 필수
	private int age; // 필수
	private String job; // 선택
	private String hobby; // 선택

	public Person(String name, int age, String job, String hobby) {
		this.name = name;
		this.age = age;
		this.job = job;
		this.hobby = hobby;
	}
	
	// 이런 생성자는 사용자가 설정하길 원치 않는 매개변수까지 포함하기 쉬운데, 
	// 어쩔 수 없이 hobby 매개변수에도 ""라는 값을 지정해줘야 한다.
	public Person(String name, int age, String job) {
		this(name, age, job, "");
	}

	public Person(String name, int age) {
		this(name, age, "", "");
	}
}

코드를 읽을 때 각 값의 의미가 무엇인지 헷갈릴 것이고, 매개변수가 몇 개인지도 주의해서 세어 보아야 한다.

또한 타입이 같은 매개변수가 연달아 늘어서 있으면 찾기 어려운 버그로 이어 질 수 있다.

클라이언트가 실수로 매개변수의 순서를 바꿔 건네줘도 컴파일러 는 알아채지 못하고, 결국 런타임에 엉뚱한 동작을 하게 된다(아이템 51).

 

 

2. 위 문제점에 대한 대안으로는 자바빈즈 패턴(JavaBeans pattern)이 있다. 

매개변수가 없는 생성자로 객체를 만든 후, 세터(setter) 메서드들을 호출해 원하는 매개변수의 값을 설정하는 방식이다.

@Setter
class Person {

	private String name = ""; // 필수
	private int age = 0; // 필수
	private String job = ""; // 선택
	private String hobby = ""; // 선택

	public Person() {
	}
}

public class DemoApplication {

	public static void main(String[] args) {
		Person person=new Person();
		person.setName("soyeon");
		person.setAge(25);
		person.setJob("student");
		person.setHobby("exercise");
	}

}

자바빈즈 패턴의 경우 장점은 코드가 길어지긴 했지만 인스턴스를 만들기 쉽고, 그 결과 더 읽기 쉬운 코드가 되었다는 점이다.

하지만 단점은 객체 하나를 만들기 위해 많은 메서드를 호출해야 하며, 객체가 완전히 생성되기 전까지는 일관성(consistency)이 무너진 상태에 놓이게 된다. => 버그가 존재하는 코드와 그 버그 때문에 런타임에 문제를 겪는 코드가 물리적으로 멀리 떨어져 있을 것이므로 디버깅이 어려워진다.

 

점층적 생성자 패턴에서는 매개변수들이 유효한지를 생성자에서만 확인하면 일관성을 유지할 수 있었는데, 그 장치가 완전히 사라진 것이다. 이처럼 일관성이 무너지는 문제 때문에 자바빈즈 패턴에서는 클래스를 불변(Item17)으로 만들 수 없으며 스레드 안전성이 없다는 문제도 있다.

 

 

3. 마지막 대안은 점층적 생성자 패턴의 안전성과 자바 빈즈 패턴의 가독성을 겸비한 빌더 패턴(Builder pattem)[Gamma95]이다.

클라이언트는 필요한 객체를 직접 만드는 대신, 필수 매개변수만으로 생성자 또는 정적 팩터리 메서드를 호출해 빌더 객체를 얻는다.

클라이언트는 빌더 객체가 제공하는 일종의 세터 메서드들로 원하는 선택 매개변수들을 설정한다.

마지막으로 매개변수가 없는 build 메서드를 호출해 드디어 우리에게 필요한 객체를 얻는다.(이 객체는 일반적으로 불변객체이다.)

빌더는 생성할 클래스 안에 정적 멤버 클래스로 만들어 두도록 한다.

package com.yapp.crew.domain.model;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookMark extends BaseEntity {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false)
	@Setter(value = AccessLevel.PRIVATE)
	private User user;

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(nullable = false)
	@Setter(value = AccessLevel.PRIVATE)
	private Board board;

	public static BookMarkBuilder getBuilder() {
		return new BookMarkBuilder();
	}

	public static class BookMarkBuilder { // 정적 멤버 클래스
		private User user;
		private Board board;

		public BookMarkBuilder withUser(User user) {
			this.user = user;
			return this;
		}

		public BookMarkBuilder withBoard(Board board) {
			this.board = board;
			return this;
		}

		public BookMark build() {
			BookMark bookMark = new BookMark();
			bookMark.setUser(user);
			bookMark.setBoard(board);

			return bookMark;
		}
	}
}
	private void saveBookMark(Board board, User user) {
    	// 빌더의 세터 메서드들은 빌더 자신을 반환하기 때문에 연쇄적으로 호출 할 수 있다.(method chaining)
		BookMarkBuilder bookMarkBuilder = BookMark.getBuilder();
		BookMark bookMark = bookMarkBuilder
				.withUser(user)
				.withBoard(board)
				.build();

		user.addBookMark(bookMark);
		board.addBookMark(bookMark);

		bookMarkRepository.save(bookMark);
	}

이때, 빌더의 생성자와 메서드에서 입력 매개변수를 검사하고, build 메서드가 호출하는 생성자에서 여러 매개변수에 걸친 불변식 (invariant)을 검사하도록 하면 더 좋다. 공격에 대비해 이런 *불변식을 보장하려면 빌더로부터 매개변수를 복사한 후 해당 객체 필드들도 검사해야 한다(Item50).

검사해서 잘못된 점을 발견하면 어떤 매개변수가 잘못되었는지를 자세히 알려주는 메시지를 담아 IllegalArgumentException을 던지면 된다(Item75).

 

 

빌더 패턴은 계충적= 설계된 클래스와 함께 쓰기에 좋다. 각 계층의 클래스에 관련 빌더를 멤버로 정의하자.

추상 클래스는 추상 빌더를, 구체 클래스(concrete class)는 구체 빌더를 갖게 한다.

다음은 피자의 다양한 종류를 표현 하는 계층구조의 루트에 놓인 추상 클래스다.

 

 

 

용어

  • 불변 vs 불변식
    • 불변(immutable, immutability): 어떠한 변경도 허용하지 않는다는 뜻으로, 주로 변경을 허용하는 가변(mutable) 객체와 구분하는 용도로 쓰인다. ex) String 객체
    • 불변식(invariant): 프로그램이 실행되는 동안, 혹은 정해진 기간 동안 반드시 만족해야 하는 조건을 뜻한다. 즉, 변경을 허용할 수는 있으나 주어진 조건 내에서 만 허용한다는 뜻이다.(아이템 50)
      • ex) 리스트의 크기는 반드시 0 이상 = 만약 한순간 이라도 음수 값이 된다면 불변식이 깨진 것이다.
      • ex) 기간을 표현하는 Period 클래스에서 start 필드의 값은 반드시 end 필드의 값보다 앞서야 = 두 값이 역전되면 불변식이 깨진 것이다


따라서 가변 객체에도 불변식은 존재할 수 있으며, 넓게 보면 불변은 불변식의 극단적 인 예라 할 수 있다.

클래스의 인스턴스를 생성하는 방법 2가지는 아래와 같다.

1. public 생성자

public class Person {
	public Person() {
	}
}

2. 정적 팩터리 메서드(static factory method)

public class Person {
	private static Person PERSON = new Person();
    
	private Person() { // 외부 생성 금지
	}

	public static final Person create() { // factory method
		return PERSON;
	}
}

 

정적 팩터리 메소드의 장점

1. 이름을 가질 수 있다. 

일반 생성자의 경우 매개변수와 생성자 자체 만으로는 반환될 객체의 특성을 제대로 설명하지 못한다.

정적 팩터리 메서드의 경우는 이름을 지으면서 반환될 객체의 특성을 쉽게 묘사할 수 있다.

// BigInteger.probablePrime: 함수 네이밍을 통해 '값이 소수인 Biginteger를 반환한다'는 의미를 잘 전달한다.
public static BigInteger probablePrime(int bitLength, Random rnd) {
        if (bitLength < 2)
            throw new ArithmeticException("bitLength < 2");

        return (bitLength < SMALL_PRIME_THRESHOLD ?
                smallPrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd) :
                largePrime(bitLength, DEFAULT_PRIME_CERTAINTY, rnd));
}

 

또한, 생성자 방식으로 인스턴스를 생성할 경우, 같은 매개변수로는 동일한 생성자를 사용해야하지만

정적 팩터리 메서드는 이름을 가질 수 있으므로 한 클래스에 같은 매개변수의 생성자가 여러개 필요하다면 차이를 잘 드러내는 이름을 지어줌으로써 역할 구분이 가능하다.

 

2. 호출될 때마다 인스턴스를 새로 생성하지는 않아도 된다.

*불변 클래스는 인스턴스를 미리 만들어 놓거나 새로 생성한 인스턴스를 캐싱하여 재활용함으로써 불필요한 객체 생성을 피할 수 있다.

public final class Boolean implements java.io.Serializable, Comparable<Boolean> {
	// static 자원은 JVM 클래스로더의 초기화 부분에서 할당된다.
	public static final Boolean TRUE = new Boolean(true);
	public static final Boolean FALSE = new Boolean(false);
	...
	public static Boolean valueOf(boolean b) {
		return (b ? TRUE : FALSE);
	}
}

같은 객체가 자주 요청되는 상황이고, 이 객체의 생성비용이 크다면 정적팩터리 메소드 방식으로 인스턴스를 생성하도록 한다. 

 

3. 반환 타입의 하위 타입 객체를 반환할 수 있는 능력이 있다. - 인터페이스기반 프레임워크(Item20)의 핵심기술

이 능력은 반환할 객체의 클래스를 자유롭게 선택할 수 있게 한다.

이를 응용하면 구현 클래스를 공개하지 않고도 그 객체를 반환할 수 있어 API를 작게 유지할 수 있다.

interface Person {

}

class Doctor implements Person {

	private Doctor() {
	} // 외부 생성 금지

	public static final Person create() { // 구체적인 타입을 숨길 수 있다
		return new Doctor();
	}
}

정적 팩터리 메서드를 사용하는 클라이언트는 얻은 객체를 (그 구현 클래스 가 아닌) 인터페이스만으로 다루게 된다(Item64)

 

4. 입력 매개변수에 따라 매번 다른 클래스의 객체를 반환할 수 있다. (반환 타입의 하위 타입이기만 하면)

정적팩터리 메서드를 사용하면 동적으로 적절한 타입의 객체를 반환할 수 있다.

 

심지어 다음 릴리스에서는 또 다른 클래스의 객체를 반환해도 된다.

    public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {
    	// 원소의 수에 따라 두 가지 하위 클래스 중 하나의 인스턴스를 반환
        Enum<?>[] universe = getUniverse(elementType);
        if (universe == null)
            throw new ClassCastException(elementType + " not an enum");

        if (universe.length <= 64)
            return new RegularEnumSet<>(elementType, universe);
        else
            return new JumboEnumSet<>(elementType, universe);
    }

클라이언트는 RegularEnumSet과 JumboEnumSet 클래스의 존재를 모른다.

따라서 RegularEnumSet, JumboEnumSet등을 삭제하거나 다른 클래스들을 추가하더라도 클라이언트는 팩터리가 건네주는 객체가 어느 클래스의 인스턴스인지 모르기 때문에 문제 없다. EnumSet의 하위 클래스이기만 하면 되는 것이다.

 

5. 정적 팩터리 메서드를 작성하는 시점에는 반환할 객체의 클래스가 존재하지 않아도 된다.

ex) JDBC(Java Database Connectivity): 서비스 제공자 프레임워크에서의 제공자(provider)는 서비스의 구현체다. 그리고 이 구현체들을 클라이언트에 제공하는 역할을 프레임워크가 통제하여 , 클라이언트를 구현체로부터 분리해준다.

 

서비스 제공자 프레임워크의 컴포넌트 3요소

  • 서비스 인터페이스(service interface): 구현체의 동작을 정의
  • 제공자 등록 API (provider registration API): 제공자가 구현체를 등록할 때 사용
  • 서비스 접근 API (service access API): 클라이언트가 서비스의 인스턴스를 얻을 때 사용

아래는 jdbc를 이용하여 db 커넥션을 만드는 코드이다.

public static void main(String[] args) {
	String driverName = "com.mysql.jdbc.Driver";
	String url = "jdbc:mysql://localhost:3306/test";
	String user = "root";
	String password = "soyeon";
    
	try {
		Class.forName(driverName);

		// 서비스 접근 API인 DriverManager.getConnection가 서비스 구현체(서비스 인터페이스)인 Connection 반환
		Connection connection = DriverManager.getConnection(url, user, password);

	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	}
}

그런데 Class.forName으로 드라이버 이름만 호출했는데 어떻게 DriverManager가 커넥션을 만들어서 리턴할 수 있었을까?

 

1. Class.forName(String name) 메소드에 의해 문자열로 전달되는 "com.mysql.jdbc.Driver"이라는 클래스가 메모리에 로드 된다.

2. 메모리에 로드되면서 "com.mysql.jdbc.Driver" 클래스의 static 절이 실행된다. 아래와 같이 DriverManager.registerDriver() 메소드를 통해 자기 자신을 등록한다. 즉, 이러한 이유로 Class.forName("com.mysql.jdbc.Driver") 실행시 JDBC Driver가 자동 등록된다.

http://www.docjar.com/html/api/com/mysql/jdbc/Driver.java.html

3. 등록한 JDBC Driver는 데이터베이스 Connection을 생성하는 시점에 사용되게 된다.

 

자바 가상머신이 동작을 시작하고, 코드가 실행되기 전까지는 어떤 JDBC 드라이버가 사용될 지 모르기 때문에, 동적으로 드라이버를 로딩하기 위해 리플렉션(java.lang.reflect)을 이용한다.

 

이외에도 서비스 제공자 프레임워크 패턴의 경우 여러 변형이 있는데,

 

자바5 이후는 java.util.ServiceLoader라는 범용 서비스 제공자 프레임워크가 제공되어 프레임워크를 직접 만들 필요가 거의 없어졌다.(Item59).

JDBC는 자바 5 전에 등장한 개념이므로 ServiceLoader를 사용하지 않고 위와 같이 구현되어 있다.

 

단점

1. 정적 팩터리 메서드만 제공하면 하위 클래스를 만들 수 없다.

상속을 하려면 public이나 protected 생성자가 필요한데, 정적 팩터리 메서드를 사용하는 경우 private 기본생성자를 통해 외부 생성을 막아두기 때문이다. 따라서 정적 팩터리 메서드를 이용하는 대표적인 클래스인 컬렉션 프레임워크의 유틸리티 구현 클래스들은 상속할 수 없 다.

이 제약은 상속보다 컴포지션을 사용(Item18) 하도록 유도하고 불변 타입으로 만들려면 이 제약을 지켜야 한다는 점에서 오히려 장점이 될 수 있다.

 

2. 정적 팩터리 메서드는 프로그래머가 찾기 어렵다. 

생성자처럼 API 설명에 명확히 드러나지 않으므로 사용자는 정적 팩터리 메서드 방식 클래스를 인스턴스화할 방법을 API 문서를 통해 알아내야 한다. 

아래는 정적 팩터리 메서드에서 통용되는 명명 방식이다.

  • from: 매개변수를 하나 받아서 해당 타입의 인스턴스를 반환하는 형변환 메서드
    • Date d = Date.from(instant);
  • of: 여러 매개변수를 받아 적합한 타입의 인스턴스를 반환하는 집계 메서드
    • Set<Rank> cards = EnumSet.of(JACK, QUEEN, KING);
  • valueOf: from과 of의 더 자세한 버전
    • Boolean true = Boolean.valueOf(true);
  • instance (getlnstance): (매개변수를 받는다면) 매개변수로 명시한 인스턴스를 반환하지만, 같은 인스턴스임을 보장하지는 않는다.
    • Calendar calendar = Calendar.getlnstance(zone);
public static Calendar getInstance(TimeZone zone){
        return createCalendar(zone, Locale.getDefault(Locale.Category.FORMAT));
}
  • create (newlnstance): instance 혹은 getlnstance와 같지만, 매번 새로 운 인스턴스를 생성해 반환함을 보장한다.
    • Object newArray = Array.newInstance(classObject, arrayLen);
public static Object newInstance(Class<?> componentType, int length) throws NegativeArraySizeException {
        return newArray(componentType, length);
}
@HotSpotIntrinsicCandidate
private static native Object newArray(Class<?> componentType, int length)
        throws NegativeArraySizeException;
  • getType: getlnstance와 같으나, 생성할 클래스가 아닌 다른 클래스에 팩터리 메서드를 정의할 때 사용한다. Type은 팩터리 메서드가 반환할 객체의 타입 이다.
    • Filestore fs = Flies.getFileStore(path)
  • newType: newlnstance와 같으나, 생성할 클래스가 아닌 다른 클래스에 팩터리 메서드를 정의할 때 쓴다. Type은 팩터 리 메서드가 반환할 객체의 타입 이다.
    • BufferedReader br = Files.newBufferedReader(path);
  • type. getType과 newType의 간결한 버전
    • List<Complaint> litany = Collections.list(legacyLitany);

 

용어

  • 불변클래스(immutable class): Item17
    • 객체가 생성된 이후 그 값을 변경할 수 없는 것 ex) String class, Wrapper class(Integer, Double, ...)
    • 하나의 상태만을 가지고 있으므로 데이터를 신뢰할 수 있다. => Thread-safe이므로 멀티스레드 환경에서 안전하다.
    • 불변클래스에서 동치인 인스턴스가 단 하나뿐임을 보장할 수 있다: a==b <=> a.equals(b)
  • 플라이웨이트 패턴(Flyweight pattern): 2021/01/19 - [책읽기/Design Patterns] - Flyweight Pattern
  • 인스턴스 통제(instance-controlled) 클래스: 인스턴스를 N개로 제어하고 있는 클래스
  • 리플렉션(java.lang.reflect): 코드 작성 시점에는 구체적인 클래스 타입을 알지 못해도, 런타임에 그 클래스의 메소드/타입/변수들을 접근할 수 있도록 해주는 java api
    • 2021/01/23 - [작성중...] - Java Reflection이란?
    • ex) annotations: 바로 설계할 때에는 어떤 타입의 클래스를 사용하는지 알 수 없으나, 실행 시점에 확인하여 사용 가능
    • java class 파일은 바이트 코드로 컴파일 되어 static 영역에 위치하게 되는데, 클래스 이름을 알게 되면 이 영역에서 클래스에 대한 정보를 가져올 수 있다.
      • className
      • class modifiers(public, private, synchronized 등)
      • package
      • superclass
      • implemented interfaces
      • constructors
      • method fields
      • annotations
  • 컴포지션: 서브 클래스의 private 필드로 Super class의 인스턴스를 참조하는 방식을 말한다.

+ Recent posts