SerializationUtils 클래스 살펴보기

Apache Commons Lang 컴포넌트의 SerializationUtils 클래스의 clone 메소드에 대해 살펴보자. clone 메소드를 이용하기 위해선 Serializable 인터페이스를 구현해줘야한다.

기존의 Object 클래스의 clone 메소드를 오버라이드 한 예제이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Main {
public static void main(String[] args) {
try {
붕어빵 a = new 붕어빵();
붕어빵 b = (붕어빵) a.clone();
System.out.println(a.price);
System.out.println(b.price);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
class 붕어빵 implements Cloneable {
public int price = 200;
@Override
protected Object clone() throws CloneNotSupportedException {
붕어빵 obj = (붕어빵) super.clone();
obj.price = price;
return obj;
}
}

SerializtionUtils 클래스의 clone 메소드를 이용한 예제이다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.io.Serializable;
import org.apache.commons.lang3.SerializationUtils;
public class Main {
public static void main(String[] args) {
붕어빵 a = new 붕어빵();
붕어빵 b = SerializationUtils.clone(a);
System.out.println(a.price);
System.out.println(b.price);
}
}
class 붕어빵 implements Serializable {
public int price = 200;
}

우와~ 이 얼마나 단순한가;; 난 이런거 좋아 ㅎㅎ

Share Comments