#10 effective java: item 3
This commit is contained in:
BIN
effective-java/elvis.obj
Normal file
BIN
effective-java/elvis.obj
Normal file
Binary file not shown.
@@ -0,0 +1,28 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
public class Concert {
|
||||||
|
|
||||||
|
private boolean lightsOn;
|
||||||
|
|
||||||
|
private boolean mainStateOpen;
|
||||||
|
|
||||||
|
private IElvis elvis;
|
||||||
|
|
||||||
|
public Concert(IElvis elvis) {
|
||||||
|
this.elvis = elvis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void perform() {
|
||||||
|
mainStateOpen = true;
|
||||||
|
lightsOn = true;
|
||||||
|
elvis.sing();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLightsOn() {
|
||||||
|
return lightsOn;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isMainStateOpen() {
|
||||||
|
return mainStateOpen;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
import java.io.Serial;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
// 코드 3-1 public static final 필드 방식의 싱글턴 (23쪽)
|
||||||
|
public class Elvis implements IElvis, Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 싱글톤 오브젝트
|
||||||
|
*/
|
||||||
|
public static final Elvis INSTANCE = new Elvis();
|
||||||
|
private static boolean created;
|
||||||
|
|
||||||
|
private Elvis() {
|
||||||
|
if (created) {
|
||||||
|
throw new UnsupportedOperationException("can't be created by constructor.");
|
||||||
|
}
|
||||||
|
|
||||||
|
created = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void leaveTheBuilding() {
|
||||||
|
System.out.println("Whoa baby, I'm outta here!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sing() {
|
||||||
|
System.out.println("I'll have a blue~ Christmas without you~");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 이 메서드는 보통 클래스 바깥(다른 클래스)에 작성해야 한다!
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Elvis elvis = Elvis.INSTANCE;
|
||||||
|
elvis.leaveTheBuilding();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Serial
|
||||||
|
private Object readResolve() {
|
||||||
|
return INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
|
||||||
|
// 생성자로 여러 인스턴스 만들기
|
||||||
|
public class ElvisReflection {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
Constructor<Elvis> defaultConstructor = Elvis.class.getDeclaredConstructor();
|
||||||
|
defaultConstructor.setAccessible(true);
|
||||||
|
Elvis elvis1 = defaultConstructor.newInstance();
|
||||||
|
Elvis elvis2 = defaultConstructor.newInstance();
|
||||||
|
|
||||||
|
Elvis.INSTANCE.sing();
|
||||||
|
} catch (InvocationTargetException | NoSuchMethodException | InstantiationException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
// 역직렬화로 여러 인스턴스 만들기
|
||||||
|
public class ElvisSerialization {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try (ObjectOutput out = new ObjectOutputStream(new FileOutputStream("elvis.obj"))) {
|
||||||
|
out.writeObject(Elvis.INSTANCE);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
try (ObjectInput in = new ObjectInputStream(new FileInputStream("elvis.obj"))) {
|
||||||
|
Elvis elvis3 = (Elvis) in.readObject();
|
||||||
|
System.out.println(elvis3 == Elvis.INSTANCE);
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
public interface IElvis {
|
||||||
|
|
||||||
|
void leaveTheBuilding();
|
||||||
|
|
||||||
|
void sing();
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.staticfactory;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class Concert {
|
||||||
|
|
||||||
|
public void start(Supplier<Singer> singerSupplier) {
|
||||||
|
Singer singer = singerSupplier.get();
|
||||||
|
singer.sing();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Concert concert = new Concert();
|
||||||
|
concert.start(Elvis::getInstance);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.staticfactory;
|
||||||
|
|
||||||
|
// 코드 3-2 정적 팩터리 방식의 싱글턴 (24쪽)
|
||||||
|
public class Elvis implements Singer {
|
||||||
|
private static final Elvis INSTANCE = new Elvis();
|
||||||
|
private Elvis() { }
|
||||||
|
public static Elvis getInstance() { return INSTANCE; }
|
||||||
|
|
||||||
|
public void leaveTheBuilding() {
|
||||||
|
System.out.println("Whoa baby, I'm outta here!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 이 메서드는 보통 클래스 바깥(다른 클래스)에 작성해야 한다!
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Elvis elvis = Elvis.getInstance();
|
||||||
|
elvis.leaveTheBuilding();
|
||||||
|
|
||||||
|
System.out.println(Elvis.getInstance());
|
||||||
|
System.out.println(Elvis.getInstance());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sing() {
|
||||||
|
System.out.println("my way~~~");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.staticfactory;
|
||||||
|
|
||||||
|
// 코드 3-2 제네릭 싱글톤 팩토리 (24쪽)
|
||||||
|
public class MetaElvis<T> {
|
||||||
|
|
||||||
|
private static final MetaElvis<Object> INSTANCE = new MetaElvis<>();
|
||||||
|
|
||||||
|
private MetaElvis() { }
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <E> MetaElvis<E> getInstance() { return (MetaElvis<E>) INSTANCE; }
|
||||||
|
|
||||||
|
public void say(T t) {
|
||||||
|
System.out.println(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void leaveTheBuilding() {
|
||||||
|
System.out.println("Whoa baby, I'm outta here!");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
MetaElvis<String> elvis1 = MetaElvis.getInstance();
|
||||||
|
MetaElvis<Integer> elvis2 = MetaElvis.getInstance();
|
||||||
|
System.out.println(elvis1);
|
||||||
|
System.out.println(elvis2);
|
||||||
|
elvis1.say("hello");
|
||||||
|
elvis2.say(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.staticfactory;
|
||||||
|
|
||||||
|
public interface Singer {
|
||||||
|
|
||||||
|
void sing();
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class ConcertTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void perform() {
|
||||||
|
Concert concert = new Concert(new MockElvis());
|
||||||
|
concert.perform();
|
||||||
|
|
||||||
|
assertTrue(concert.isLightsOn());
|
||||||
|
assertTrue(concert.isMainStateOpen());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.effectivejava.chapter01.item03.field;
|
||||||
|
|
||||||
|
public class MockElvis implements IElvis {
|
||||||
|
@Override
|
||||||
|
public void leaveTheBuilding() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sing() {
|
||||||
|
System.out.println("You ain't nothin' but a hound dog.");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user