반응형

규칙 13에서는 소프트웨어 설계의 기본적인 원칙 가운데 하나인 캡슐화에 관해서 설명한다.

 

캡슐화는 다른 모듈 간 커플링을 최소화하기 위하여 구현 세부사항을 전부 API 뒤쪽에 숨긴다. 그리고 모듈들은 이 API를 통해서만 서로를 호출한다. 디커플링을 하게 되면 서로 간에 의존성이 낮아지기 때문에 한쪽이 변경되어도 다른 모듈에 영향을 끼칠 걱정이 없다. 또한 다른 개발에서도 유용하게 쓰일 수 있다는 장점이 있다. 자바는 이런 장점을 사용하기 위해 캡슐화를 지원하며 그중 대표적으로 접근 제어(Access Control) 기능을 제공한다.

 

원칙은 단순하다. 가능한 한 다른 곳에서 접근 불가능하도록 만들어라. 클라이언트에게 제공하는 API만 public 또는 protected로 하라.

 

 

1. 클래스와 인터페이스에 public을 붙이지 않을 수 있는지 확인해보자.

public으로 한번 릴리스한 클래스와 인터페이스는 호환성을 보장하기 위해 계속 제공해야 한다. (대부분 지키지 않지만)

 

예를 들어 자바에서는 Enum에 특화된 Set을 제공하는 EnumSet을 제공하는데, EnumSet을 public으로 제공하여 누구나 사용할 수 있다. 하지만 실제로 정적 팩토리 메서드가 제공하는 객체는 RegularEnumSet, JumboEnumSet이 있는데 이 둘은 직접적으로 사용할 수 없도록 package private 접근 제어자를 사용한다. 즉 jdk 개발자는 RegularEnumSet 클래스를 변경하던, 삭제하던 호환성에 대해 걱정할 필요 없다. 

// EnumSet은 public으로 추상 클래스 제공
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>
    implements Cloneable, java.io.Serializable
{
	...
    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);
    }
	...
}

// package private
class RegularEnumSet<E extends Enum<E>> extends EnumSet<E> {
	...
}

 

2. 클래스의 멤버 변수들은 최대한 package-private까지만 지원하자. protected도 광범위하다.

객체 필드변수는 절대로 public으로 선언하면 안 된다. 필드를 public으로 선언하면 필드에 저장될 값을 제한할 수 없게 되어 그 필드에 관계된 불변식을 강제할 수 없다. 그러므로 getter와같은 접근자 메서드를 사용하는 게 좋다.

 

단, 상수는 public static final로 선언될 수 있다. 아래는 BigDeimal의 상수 예시이다.

    // Cache of common small BigDecimal values.
    private static final BigDecimal ZERO_THROUGH_TEN[] = {
        new BigDecimal(BigInteger.ZERO,       0,  0, 1),
        new BigDecimal(BigInteger.ONE,        1,  0, 1),
        new BigDecimal(BigInteger.TWO,        2,  0, 1),
        new BigDecimal(BigInteger.valueOf(3), 3,  0, 1),
        new BigDecimal(BigInteger.valueOf(4), 4,  0, 1),
        new BigDecimal(BigInteger.valueOf(5), 5,  0, 1),
        new BigDecimal(BigInteger.valueOf(6), 6,  0, 1),
        new BigDecimal(BigInteger.valueOf(7), 7,  0, 1),
        new BigDecimal(BigInteger.valueOf(8), 8,  0, 1),
        new BigDecimal(BigInteger.valueOf(9), 9,  0, 1),
        new BigDecimal(BigInteger.TEN,        10, 0, 2),
    };

	public static final BigDecimal ZERO = zeroThroughTen[0];
	public static final BigDecimal ONE = zeroThroughTen[1];
	public static final BigDecimal TEN = zeroThroughTen[10];

 

하지만 상수라도 기본 자료형 값들을 갖거나, 변경 불가능 객체를 참조하게 해야 한다.

public static final Thing[] VALUES = { ... };

위 코드와 같이 길이가 0이 아닌 배열 상수 필드는 배열 레퍼런스는 변경하지 못하지만 배열 레퍼런스 안에 존재하는 값은 변경할 수 있다. 따라서 public static final 배열 필드를 두거나, 배열 필드를 반환하는 접근자를 정의하면 안 된다.

 

따라서 클라이언트에게는 배열 필드의 상수 값을 복사해서 넘겨주는 방식으로 이 문제를 해결한다.

방법 1.

	private static final String[] UNMODIFY_NUM_LIST = { "1", "2", "3", "4" };
	public static final List<String> NUM_LIST_2 = Collections.unmodifiableList(Arrays.asList(UNMODIFY_NUM_LIST));

방법 2.

	private static final String[] CLONE_NUM_LISE = { "1", "2", "3", "4" };
	public static final String[] NUM_LIST_3() {
		return CLONE_NUM_LISE.clone();
	}

 

3. 클래스의 메서드도 최대한 접근 권한을 줄이자.

클래스의 메서드도 최대한 접근 권한을 줄여야 한다. 하지만 접근 권한을 줄일 수 없는 경우가 있다. 상위 클래스 메서드를 오버라이딩할 때는 원래 메서드의 접근 권한보다 낮은 권한을 설정할 수 없다.

 

 

결론은 접근 권한은 가능한 낮춰라.

 

반응형
반응형

최상위 객체인 Object에 선언되어 있는 메서드는 아니지만 자주 사용되는 compareTo 메서드에 대해 설명한다.

 

int java.lang.String.compareTo(String anotherString)

자바에서 기본적으로 제공하는 값 클래스(value class)는 compareTo 메서드가 구현되어 있으며 또한 String 클래스에도 compareTo가 구현되어 있다. String.compareTo 메서드는 문자열을 비교하여 같으면 0을, 사전순으로 작으면 음수를, 사전순으로 크면 양수를 리턴한다.

	public static void main(String[] args) {
		String str1 = "apple";

		System.out.println(str1.compareTo("banana")); // -1
		System.out.println(str1.compareTo("apple")); // 0
		System.out.println(str1.compareTo("abc")); // 14
	}

 

String.compareTo 메서드의 소스코드는 아래와 같다.

    public int compareTo(String anotherString) {
        int len1 = value.length;
        int len2 = anotherString.value.length;
        int lim = Math.min(len1, len2);
        char v1[] = value;
        char v2[] = anotherString.value;

        int k = 0;
        while (k < lim) {
            char c1 = v1[k];
            char c2 = v2[k];
            if (c1 != c2) {
                return c1 - c2;
            }
            k++;
        }
        return len1 - len2;
    }

1. 문자를 하나씩 비교하여 다를 경우 차를 구해 바로 리턴한다.

2. 길이가 작은 문자열의 길이 만큼 비교하여도 같다면 문자열의 길이가 작은게 사전순으로 더 앞에 있다고 판단하고 차를 구해 리턴한다.

 

이렇게 compareTo 메서드는 해당 클래스 객체들의 자연적 순서를 비교할 수 있도록 기능을 제공한다. 그리고 다들 알겠지만 compareTo는 일반적인 메서드가 아닌 Comparable 인터페이스에 정의되어 있는 유일한 메서드이다.

 

Comparable 인터페이스

Comparable 인터페이스는 클래스 객체에 순서를 정의하도록 한다. 이 순서를 클래스의 자연 순서라고 하며 클래스의 compareTo 메소드는 자연 순서를 계산할 수 있도록 기능을 구현해야 한다. 그래서 위와 같이 String 클래스는 Compareable 인터페이스를 구현하였고, 자연 순서를 계산할 수 있도록 compareTo 메서드를 구현한 것이다.

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {
	...
}

 

규칙9에서 설명했던 hashCode는 규약을 따르지 않으면 hashCode를 사용하는 자바 API(ex : HashMap) 동작에 문제가 발생한다고 설명했다. 마찬가지로 Comparable의 compareTo 메서드를 사용하는 자바 API도 여러 개 존재하는데 compareTo 규약을 준수하지 않을 경우 API를 오동작시킬 수 있다. 아래는 compareTo를 사용하는 API의 예시이다.

 

- void java.util.Arrays.sort(Object[] a)

	public static void arraySort() {
		String arr[] = { "banana", "apple", "google", "aws" };

		Arrays.sort(arr);
		System.out.printf("%s\n", Arrays.toString(arr)); // [apple, aws, banana, google]

		Arrays.sort(arr, Collections.reverseOrder());

		System.out.printf("%s\n", Arrays.toString(arr)); // [google, banana, aws, apple]
	}

 

- java.util.TreeSet<E>

	public static void treeSet() {
		String arr[] = { "banana", "apple", "google", "aws" };

		Set<String> s = new TreeSet<String>();
		s.addAll(Arrays.asList(arr));

		System.out.println(s); // [apple, aws, banana, google]
	}

 

그렇다면 이제 compareTo를 잘 구현해야한다. 규약을 요약하자면 아래와 같다.

 

- Symmetry(대칭성)

X가 Y보다 크다면 Y는 X보다 작아야 한다. 반대로 X가 Y보다 작다면 Y는 X보다 커야 한다. 또한 X가 Y랑 같다면 Y도 X랑 같아야 한다.

 

- Transitivity(추이성)

X가 Y보다 크고, Y가 Z보다 크다면 X는 Z보다 커야한다.

 

- Consistent(일관성)

X와 Y가 같다면 X와 Z와의 비교 결과와 Y와 Z의 비교 결과는 항상 같다.

 

(이펙티브 자바 책에서는 위 순서를 반사성, 대칭성, 추이성이라고 표현하지만, 필자는 잘못되었다고 생각한다.)

 

- (강력히 추천하지만 절대적으로 요구되는 것은 아닌) X와 Y의 compareTo의 결과가 0이라면 equlas의 결과는 true여야 한다.

네번째 규약은 특별한 경우가 아니라면 지키는게 좋다. 자바에서 제공하는 대부분의 컬렉션 구현체들은 마지막 규약이 지켜졌다고 생각하고 동작한다. 만약 규약을 지키지 않으면 엄청난 재앙을 가져오진 않지만, 그래도 약간의 문제가 발생할 수 있다.

 

네번째 규약을 지키지 않은 대표적인 클래스가 있는데 예시는 아래와 같다.

	public static void main(String[] args) {
		BigDecimal a = new BigDecimal("1.0");
		BigDecimal b = new BigDecimal("1.00");

		System.out.println(a.equals(b)); // false
		System.out.println(a.compareTo(b)); // 0
		System.out.println((a.compareTo(b) == 0) == a.equals(b)); // false

		Set<BigDecimal> hashSet = new HashSet<BigDecimal>();
		hashSet.add(a);
		hashSet.add(b);

		System.out.println(hashSet.size()); // 2

		Set<BigDecimal> treeSet = new TreeSet<BigDecimal>();
		treeSet.add(a);
		treeSet.add(b);

		System.out.println(treeSet.size()); // 1
	}

BigDecimal은 소수점까지 모두 동일해야지 동일한 것으로 판단한다. 하지만 compare는 크기를 비교하므로 0을 리턴한다. 따라서 네번 째 규약을 지키지 않는 클래스이다. 이렇기 때문에 equals(hashCode)로 동일성을 판단하는 HashSet에서는 두 원소를 넣어도 다르다고 판단하여 두 개의 객체가 존재한다. 하지만 TreeSet은 compareTo로 비교하기 때문에 두 객체는 동일하다고 판단하여 하나의 객체만 존재하게 된다.

 

근데 만약 이미 구현되어 있는 클래스의 compareTo 순서 비교 방식이 아닌 새로운 순서 비교 방식을 사용하고 싶다면 어떻게 해야할까? 이럴 경우 Comparator 인터페이스를 구현하여 사용하면 된다.

 

Comparator 인터페이스

일반적으로 Comparable.compareTo를 사용하는 메서드는 Comparator의 compare 메서드를 사용할 수 있도록 구현되어있다. (ex : Collections.sort 또는 Arrays.sort) 따라서 미리 구현되어 있는 compareTo 방식이 아닌 새로운 방식으로 순서를 지정하고 싶다면 Comparator를 구현하면 된다.

 

아래는 예시이다.

	public static void main(String[] args) {
		String arr[] = { "banana", "apple", "google", "aws" };

		Arrays.sort(arr, new Comparator<String>() {
			public int compare(String o1, String o2) {
				return o2.compareTo(o1); // o1을 o2와 비교한 것이 아닌 o2를 o1과 비교
			}
		});

		System.out.printf("%s\n", Arrays.toString(arr)); // [google, banana, aws, apple]

	}

 

Arrays.sort에 새롭게 구현한 Comparator 구현체를 사용하였다. 자세히 보면 기존 o1.compareTo(o2)가 아닌 o2.compareTo(o1)을 사용하여 사전순이 아닌 역사전순으로 비교하도록 하였다.

 

이렇게 자신이 원하는 순서를 다시 재정의할 수 있는데, 자바에서는 자주 사용하는 비교 방식의 Comparator를 미리 제공한다. 위에 예시였던 arraySort() 메서드에서 보면 Collections.reverseOrder()를 사용하였는데, 이는 우리가 지금 구현한 Comparator와 동일하게 구현되어 있는 구현체이다. 이 외에도 String 클래스에서는 대소문자를 무시하여 비교하는 구현체를 제공한다. 아래는 해당 소스이다.

    public static final Comparator<String> CASE_INSENSITIVE_ORDER
                                         = new CaseInsensitiveComparator();
    private static class CaseInsensitiveComparator
            implements Comparator<String>, java.io.Serializable {
        // use serialVersionUID from JDK 1.2.2 for interoperability
        private static final long serialVersionUID = 8575799808933029326L;

        public int compare(String s1, String s2) {
            int n1 = s1.length();
            int n2 = s2.length();
            int min = Math.min(n1, n2);
            for (int i = 0; i < min; i++) {
                char c1 = s1.charAt(i);
                char c2 = s2.charAt(i);
                if (c1 != c2) {
                    c1 = Character.toUpperCase(c1);
                    c2 = Character.toUpperCase(c2);
                    if (c1 != c2) {
                        c1 = Character.toLowerCase(c1);
                        c2 = Character.toLowerCase(c2);
                        if (c1 != c2) {
                            // No overflow because of numeric promotion
                            return c1 - c2;
                        }
                    }
                }
            }
            return n1 - n2;
        }

        /** Replaces the de-serialized object. */
        private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
    }

 

반응형
반응형
column = {

   ...,
   sortMode : natural
}

default : 문자열로 정렬


natural : 사람에게 더 자연스럽게 느껴지는 방식으로 정렬한다. (ex : IP 정렬, 숫자 정렬 등)


i18n : 발음 구별 부호를 고려하여 정렬한다. 

반응형

'프론트' 카테고리의 다른 글

[cleave.js] time 최대 값 변경하기 (23:59:59 -> 24:00:00)  (0) 2022.02.21
반응형

이 포스팅은 토비의 스프링 3.1을 읽고 글쓴이가 좀 더 이해하기 쉽도록 정리한 내용입니다.

이 포스팅의 대상은 토비의 스프링을 읽어보신 분들이 한 번쯤 다시 기억을 상기시키고자 하는 분들입니다. 

글쓴이가 이미 알고 있는 내용에 대해서는 생략합니다. 또한 소스코드도 생략이 많으니 유의해서 읽어주시기 바랍니다.


1장에서 봤던 디자인 패턴 중 하나인 템플릿 메서드 패턴과 같이 템플릿이란 코드 중에서 변경이 자주 일어나는 부분과 코드를 독립시켜 변경이 거의 일어나지 않는 부분을 만들어 활용하는 방법을 칭한다.

 

3.1 다시 보는 초난감 DAO

 

우리는 DAO에서 1. 커넥션을 얻고, 2. PreparedStatement로 쿼리를 수행하고 3. 리소스를 회수했다.

public void deleteAll() throws SQLException, ClassNotFoundException {
		// 1. 커넥션 얻기
		Connection c = dataSource.getConnection();

		// 2. 쿼리 수행
		PreparedStatement ps = c.prepareStatement("delete from users");
		ps.executeUpdate();

		// 3. 리소스 회수
		ps.close();
		c.close();
	}

그런데 왜 리소스를 회수해야 할까?

 

일반적으로 DB를 사용할 때 커넥션을 매번 생성하지 않는다. 만약 DB 요청이 많은 상황에서 DB 연결을 위한 커넥션을 매번 생성한다면 다른 작업도 수행해야 하는데 커넥션도 계속 생성해야 하므로 더욱 악순환만 반복되게 된다.

 

위와 같은 문제점을 해결하기 위해 바구니와 같은 풀(Pool) 안에 커넥션을 미리 만들어 두고, 요청이 들어올 때 풀에서 꺼내 해당 커넥션을 사용하도록 한다. 풀 방식을 사용하면 많은 요청이 들어와도 미리 만들어 둔 커넥션을 사용하게 하면 되기 때문에 다른 작업을 더 빠르게 할 수 있다. 또한 풀 안에 더 이상 사용 가능한 커넥션이 없다면 나머지 요청은 대기하게 할 수 있기 때문에 부하를 줄일 수 있다.

 

다만 아무도 풀에서 가지고 간 커넥션을 반환하지 않으면 어떻게 될까? 더 이상 요청을 처리하지 못하고 결국 문제가 발생한다. 따라서 모든 작업을 완료했다면 리소스를 풀로 반드시 돌려줘야 한다. 이러한 이유로 우리는 3. 리소스 회수를 수행했던 것이다.

 

하지만 위 코드에서 3. 리소스 회수가 수행되지 못할 수가 있다. 3. 리소스 회수 전에 있는 코드에서 예외가 발생하면 메서드 실행을 끝마치지 못하고 메서드를 빠져나가게 되므로 리소스를 반환하지 못한다.

 

그래서 일반적으로 JDBC 코드에서는 아래와 같이 try/catch/finally 코드로 작성한다.

public void deleteAll() throws SQLException {
		Connection c = null;
		PreparedStatement ps = null;

		try {
			// 1. 커넥션 얻기
			c = dataSource.getConnection();
			
			// 2. 쿼리 수행
			ps = c.prepareStatement("delete from users");
			ps.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 3. 리소스 회수
			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) { 

			}

			try {
				if (c != null) {
					c.close();
				}
			} catch (SQLException e) {

			}
		}
	}

finally는 정상 수행하든 예외가 발생하든 반드시 수행되므로 해당 블록에서 리소스 반환을 하면 된다.

예외가 어느 위치에서 발생하냐에 따라 ps와 c가 null이 될 수 있다. 따라서 NPE를 방지하기 위해 null 체크를 해준다.

 

close() 메서드도 예외가 발생할 수 있다. 만약 아래와 같이 작성하였을 때 문제점은 무엇일까?

public void deleteAll() throws SQLException {
		...

		} finally {
			if (ps != null) {
				ps.close();
			}

			if (c != null) {
				c.close();
			}
			
		}
	}

ps.close에서 예외가 발생한다면 c.close()가 호출되지 않고 끝나게 된다. 따라서 c의 리소스는 반환하지 않은 채로 남게 되는 문제가 발생한다. 따라서 이를 방지하기 위해 close() 메서드도 try/catch로 묶어서 예외가 발생해도 아래 코드가 수행되도록 한다.

 

지금까지 봤던 delete 코드는 리소스를 Connection과 PreparedStatement를 사용하여 반환하였다. 데이터를 조회하는 코드는 두 리소스뿐만 아니라 ResultSet 리소스를 추가적으로 사용하기 때문에 해당 리소스도 반환해줘야 한다.

예를 들어 데이터 단건을 조회하는 기존 코드는 다음과 같다.

public User get(String id) throws ClassNotFoundException, SQLException {
		Connection c = dataSource.getConnection();

		PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
		ps.setString(1, id);

		ResultSet rs = ps.executeQuery();
		rs.next();

		User user = new User();
		user.setId(rs.getString("id"));
		user.setName(rs.getString("name"));
		user.setPassword(rs.getString("password"));

		rs.close();
		ps.close();
		c.close();

		return user;
	}

이제 이 코드도 try/catch/finally로 바꾸면 아래와 같다.

public User get(String id) throws ClassNotFoundException, SQLException {
		Connection c = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			c = dataSource.getConnection();

			ps = c.prepareStatement("select * from users where id = ?");
			ps.setString(1, id);

			rs = ps.executeQuery();
			rs.next();

			User user = new User();
			user.setId(rs.getString("id"));
			user.setName(rs.getString("name"));
			user.setPassword(rs.getString("password"));

			return user;
		} catch (SQLException e) {
			throw e;
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (SQLException e) {

			}

			try {
				if (ps != null) {
					ps.close();
				}
			} catch (SQLException e) {

			}

			try {
				if (c != null) {
					c.close();
				}
			} catch (SQLException e) {

			}
		}

	}

완벽하게 동작하는 JDBC 코드를 완성하였다. 하지만 코드가 좀 그렇지 않은가..?

반응형
반응형

자바에서 최상위 객체인 Object는 하위 객체들이 오버라이딩하여 사용하도록 설계된 메서드들이 있다. (equals, hashCode, toString, clone, finalize) 그리고 이 메서드들은 일반 규약이 존재하는데 이를 따르지 않으면 자바에서 제공하는 클래스와 함께 사용할 때 제대로 동작하지 않는다.

 

이번 장에서는 clone 메서드에 대해 설명한다.

 

Cloneable 인터페이스

clone 메서드는 Object 클래스에 정의되어 있고, 하위 객체가 오버라이딩하여 재정의하여 사용한다. 하지만 clone 메서드를 정상적으로 사용하기 위해서는 추가적으로 해당 클래스가 Cloneable 인터페이스를 implements 해야한다.

 

사실 이런 구현방식은 정말 기괴한 방식이다. Cloneable 인터페이스에 clone 메서드가 정의만 되어 있었어도 Cloneable 인터페이스를 구현하는 클래스는 강제적으로 clone 메서드를 구현해야 하기때문에 아주 상식적인 방식으로 clone의 동작 방식을 이해할 수 있었을 것이다.

 

하지만 자바는 Cloneable을 Marker Interface로 구현하였고, natvie 메서드인 Object.clone()에서 instanceof를 사용하여 Cloneable을 implement한 클래스인 경우 clone 메서드를 호출하고, Cloneable를 implement하지 않은 클래스인 경우 CloneNotSupportedException 예외를 던지도록 구현되어 있다.

 

아래는 Cloneable의 소스이다.

package java.lang;

/**
 * A class implements the <code>Cloneable</code> interface to
 * indicate to the {@link java.lang.Object#clone()} method that it
 * is legal for that method to make a
 * field-for-field copy of instances of that class.
 * <p>
 * Invoking Object's clone method on an instance that does not implement the
 * <code>Cloneable</code> interface results in the exception
 * <code>CloneNotSupportedException</code> being thrown.
 * <p>
 * By convention, classes that implement this interface should override
 * <tt>Object.clone</tt> (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this
 * method.
 * <p>
 * Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
 * Therefore, it is not possible to clone an object merely by virtue of the
 * fact that it implements this interface.  Even if the clone method is invoked
 * reflectively, there is no guarantee that it will succeed.
 *
 * @author  unascribed
 * @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0
 */
public interface Cloneable {
}

 

Object.clone() 메서드

위에서 언급한 것처럼 Object.clone의 동작방식은 기괴하지만 널리 사용되고 있으므로 알아둬야 한다. 먼저 Object.clone() 메서드는 아래와 같다.

    /**
     * Creates and returns a copy of this object.  The precise meaning
     * of "copy" may depend on the class of the object. The general
     * intent is that, for any object {@code x}, the expression:
     * <blockquote>
     * <pre>
     * x.clone() != x</pre></blockquote>
     * will be true, and that the expression:
     * <blockquote>
     * <pre>
     * x.clone().getClass() == x.getClass()</pre></blockquote>
     * will be {@code true}, but these are not absolute requirements.
     * While it is typically the case that:
     * <blockquote>
     * <pre>
     * x.clone().equals(x)</pre></blockquote>
     * will be {@code true}, this is not an absolute requirement.
     * <p>
     * By convention, the returned object should be obtained by calling
     * {@code super.clone}.  If a class and all of its superclasses (except
     * {@code Object}) obey this convention, it will be the case that
     * {@code x.clone().getClass() == x.getClass()}.
     * <p>
     * By convention, the object returned by this method should be independent
     * of this object (which is being cloned).  To achieve this independence,
     * it may be necessary to modify one or more fields of the object returned
     * by {@code super.clone} before returning it.  Typically, this means
     * copying any mutable objects that comprise the internal "deep structure"
     * of the object being cloned and replacing the references to these
     * objects with references to the copies.  If a class contains only
     * primitive fields or references to immutable objects, then it is usually
     * the case that no fields in the object returned by {@code super.clone}
     * need to be modified.
     * <p>
     * The method {@code clone} for class {@code Object} performs a
     * specific cloning operation. First, if the class of this object does
     * not implement the interface {@code Cloneable}, then a
     * {@code CloneNotSupportedException} is thrown. Note that all arrays
     * are considered to implement the interface {@code Cloneable} and that
     * the return type of the {@code clone} method of an array type {@code T[]}
     * is {@code T[]} where T is any reference or primitive type.
     * Otherwise, this method creates a new instance of the class of this
     * object and initializes all its fields with exactly the contents of
     * the corresponding fields of this object, as if by assignment; the
     * contents of the fields are not themselves cloned. Thus, this method
     * performs a "shallow copy" of this object, not a "deep copy" operation.
     * <p>
     * The class {@code Object} does not itself implement the interface
     * {@code Cloneable}, so calling the {@code clone} method on an object
     * whose class is {@code Object} will result in throwing an
     * exception at run time.
     *
     * @return     a clone of this instance.
     * @throws  CloneNotSupportedException  if the object's class does not
     *               support the {@code Cloneable} interface. Subclasses
     *               that override the {@code clone} method can also
     *               throw this exception to indicate that an instance cannot
     *               be cloned.
     * @see java.lang.Cloneable
     */
    protected native Object clone() throws CloneNotSupportedException;

 

clone 메서드는 native 메서드로 작성되어 있으며 Cloneable 인터페이스를 구현하지 않은 클래스가 해당 메서드를 사용할 경우 CloneNotSupportedException 예외를 던지도록 정의되어 있다.

 

clone 메서드도 마찬가지로 규약이 존재하지만 다른 메서드의 비해 규약이 느슨한다.

 

1. x.clone() != x will be true, and that the expression.

객체의 복사본을 만들어서 반환하는 것이기 때문에 원본과 복사본은 동일할 수는 없다.

 

2. x.clone().getClass() == x.getClass() will be true, but these are not absolute requirements.

원본과 복사본의 클래스 타입은 동일해야하지만, 반드시 그래야 하는 것은 아니다. 하지만 이 규약은 너무 느슨하다. clone을 사용하는 입장에서는 원본과 복사본의 타입이 동일 할 것이라고 생각한다

 

3.  x.clone().equals(x) will be true, this is not an absolute requirement.

 

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

 

 

--- 추후 추가 작성 

 

반응형
반응형

이전 블로그에서 2017년 8월 7일에 작성한 글


 

대부분의 웹 서버 (Web Server)는 디폴트로 GET, POST, HEAD 이외의 요청 메소드는 막아둔다.

Restful API를 제공하는 웹 어플리케이션이 웹 서버로부터 위 세가지 이외의 요청 메소드를 받으려면 WebtoB (버전 4.1) 에서는 다음과 같이 설정해야 한다.

 

$WEBTOB_HOME/config/ws_engine.m 파일에 다음을 추가한다.

*NODE

WEBTOBDIR = ...

SHMKEY = ...

...

METHOD = "GET, POST, HEAD, DELETE, PUT"

 

설정을 변경하였으므로 wscfl ​명령어로 설정을 적용하고 WebtoB를 재부팅 한다.

 

반응형
반응형

이전 블로그에서 2017년 7월 21일에 작성한 글


Constant Interface를 위키에서 보다가 Constant Interface는 런타임 시에는 사용할 목적이 없지만, Marker Interface는 런타임 시에 사용할 목적이 있다고 표현되어 있었다. 고로 Marker Interface도 인터페이스이지만 메서드 선언이 없는 인터페이스라고는 추측은 되지만 그렇다면 어떤 용도로 사용하는지 자료를 찾아보았다.

 

Marker Interface

Marker Interface란 아무것도, 즉 변수와 메서드를 정의하지 않은 인터페이스이다. 먼저 대표적인 Marker Interface에는 ​자바에서 종종 보는 Serializable, Cloneable​ 인터페이스가 있다. 대학원을 다녔을 때, 오브젝트 정보를 파일로 저장하고 불러올 때 사용을 해봤지만 안에 어떤 것이 들어있는지 확인은 하지 않았다. 아래는 ​Cloneable​의 코드이다.

package java.lang;

/**
 * A class implements the <code>Cloneable</code> interface to
 * indicate to the {@link java.lang.Object#clone()} method that it
 * is legal for that method to make a
 * field-for-field copy of instances of that class.
 * <p>
 * Invoking Object's clone method on an instance that does not implement the
 * <code>Cloneable</code> interface results in the exception
 * <code>CloneNotSupportedException</code> being thrown.
 * <p>
 * By convention, classes that implement this interface should override
 * <tt>Object.clone</tt> (which is protected) with a public method.
 * See {@link java.lang.Object#clone()} for details on overriding this
 * method.
 * <p>
 * Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
 * Therefore, it is not possible to clone an object merely by virtue of the
 * fact that it implements this interface.  Even if the clone method is invoked
 * reflectively, there is no guarantee that it will succeed.
 *
 * @author  unascribed
 * @see     java.lang.CloneNotSupportedException
 * @see     java.lang.Object#clone()
 * @since   JDK1.0
 */
public interface Cloneable {
}

 

그렇다면 Marker Interface는 어디에 사용될까? instanceof 연산자를 사용하여 런타임에서 객체에 대한 타입을 확인하여 프로그램의 흐름을 제어할 수 있다. 즉 Maker Interface는 특별한 기능을 하는 것이 아닌 단순히 해당 객체의 타입을 구분하는 정도로 사용한다.

 

예를 들어 오브젝트를 입력받아 스트림에 쓰는 ObjectOutputStream.writeObject(Object obj)는 Object를 인자로 받는다. 이 매소드는 writeObject0(Object obj, ...)​을 호출한다. 이 매소드는 Object의 타입에 따라 다시 알맞은 매소드를 호출한다. 아래는 writeObject0(Object obj, ...)​의 코드 일부이다. (String, Enum도 Serializable이지만 처리를 다르게 하려고 구분한 것으로 보임)

public class ObjectOutputStream extends OutputStream implements ObjectOutput, ObjectStreamConstants {

 // ...
 private void writeObject0(Object obj, boolean unshared) throws IOException {

  // ...
  if (obj instanceof String) {
   writeString((String) obj, unshared);
  } else if (cl.isArray()) {
   writeArray(obj, desc, unshared);
  } else if (obj instanceof Enum) {
   writeEnum((Enum<?>) obj, desc, unshared);
  } else if (obj instanceof Serializable) {
   writeOrdinaryObject(obj, desc, unshared);
  } else {
   if (extendedDebugInfo) {
    throw new NotSerializableException(cl.getName() + "\n" + debugInfoStack.toString());
   } else {
    throw new NotSerializableException(cl.getName());
   }
  }

  // ...
 }
}
반응형
반응형

이전 블로그에서 2017년 7월 20일에 작성한 글


 

프로젝트 소스를 보다가 상수(Constant)를 인터페이스(Interface)에 정의한 것을 발견했다. 학교 수업과 책으로 자바를 배웠을 때, 항상 상수는 클래스에 정의한 것만 봐왔던 나는 생소했다. 당연히 초보 개발자로서, 특별한 장점이 있으므로 실무에서는 인터페이스를 사용하지 않을까 생각해 자료를 찾아보았다. 결론부터 말하면 사용을 추천하지 않는 Anti 패턴이다.

 

Constant Interface

Constant Interface란 오직 상수만 정의한 인터페이스이다. 인터페이스의 경우, ​변수를 등록할 때 자동으로 public static final​이 붙는다. 따라서 상수처럼 어디에서나 접근할 수 있다. 그 뿐만 아니라 하나의 클래스에 여러 개의 인터페이스를 Implement 할 수 있는데, Constant Interface를 Implement 할 경우, ​인터페이스의 클래스 명을 네임스페이스로 붙이지 않고 바로 사용할 수 있다. 이러한 편리성 때문에 Constant Interface를 사용한다. 아래는 간단한 예이다. 

public interface Constants {
 double PI = 3.14159;
 double PLANCK_CONSTANT = 6.62606896e-34;
}

public class Calculations implements Constants {

 public double getReducedPlanckConstant() {
  return PLANCK_CONSTANT / (2 * PI);
 }
}

 

Constant Interface 문제점

Constant Interface를 써도 컴파일이 안 되는 것도 아니고 그렇게 잘못된 것 같지는 않아 보인다. 하지만 위키 Effective Java (규칙19) 책을 보면 다음과 같은 이유로 Anti 패턴으로 간주한다.

 

1. Implement 할 경우 사용하지 않을 수도 있는 상수를 포함하여 모두 가져오기 때문에 계속 가지고 있어야 한다.

 

2. 컴파일할 때 사용되겠지만, 런타임에는 사용할 용도가 없다. (Marker Interface는 런타임에 사용할 목적이 있으므로 다름)

 

3. Binary Code Compatibility (이진 호환성)을 필요로 하는 프로그램일 경우, 새로운 라이브러리를 연결하더라도, 상수 인터페이스는 프로그램이 종료되기 전까지 이진 호환성을 보장하기 위해 계속 유지되어야 한다.

 

4. IDE가 없으면, 상수 인터페이스를 Implement 한 클래스에서는 상수를 사용할 때 네임스페이스를 사용하지 않으므로, 해당 상수의 출처를 쉽게 알 수 없다. 또한 상수 인터페이스를 구현한 클래스의 하위 클래스들의 네임스페이스도 인터페이스의 상수들로 오염된다.

 

5.  인터페이스를 구현해 클래스를 만든다는 것은, 해당 클래스의 객체로 어떤 일을 할 수 있는지 클라이언트에게 알리는 행위이다. 따라서 상수 인터페이스를 구현한다는 사실은 클라이언트에게는 중요한 정보가 아니다. 다만, 클라이언트들을 혼동시킬 뿐이다.

 

6. 상수 인터페이스를 Implement 한 클래스에 같은 상수를 가질 경우, 클래스에 정의한 상수가 사용되므로 사용자가 의도한 흐름으로 프로그램이 돌아가지 않을 수 있다. 아래는 간단한 예제이다.

public interface Constants {
 public static final int CONSTANT = 1;
}

public class Class1 implements Constants {

 public static final int CONSTANT = 2; // *

 public static void main(String args[]) throws Exception {
  System.out.println(CONSTANT);
 }
}

 

Constant Interface 대안

자바문서에서 Constant Interface를 Anti 패턴으로 명시하였고 이 방안으로 ​import static 구문​ 사용을 권장한다.​ Constant Interface와 동일한 기능과 편리성을 제공한다. 아래는 간단한 예제이다.

public final class Constants {
 private Constants() {
  // restrict instantiation
 }

 public static final double PI = 3.14159;
 public static final double PLANCK_CONSTANT = 6.62606896e-34;
}



import static Constants.PLANCK_CONSTANT;
import static Constants.PI;

public class Calculations {

 public double getReducedPlanckConstant() {
  return PLANCK_CONSTANT / (2 * PI);
 }
}

 

반응형

+ Recent posts