불변선언

2026년 5월 13일 09:30분

민수기 23장에서 발람은 저주하려 했지만 축복만 선언됩니다. 한 번 설정된 선언은 외부 요청으로 바꿀 수 없습니다. 나는 선언을 불변 객체로 만들고 변경 시도를 거부하는 선언 관리자를 만들었습니다.

package com.jesusbornd.numbers;

public class Numbers_23_Chapter_Lv3 {
    record Declaration(String subject, String content) {
        Declaration override(String newContent) {
            System.out.printf("⚠️ '%s' 변경 시도 — 거부됨 (불변 선언)%n", subject);
            return this;
        }
    }

    public static void main(String[] args) {
        Declaration blessing = new Declaration("이스라엘", "하나님이 복을 주셨으니 내가 돌이킬 수 없다");
        System.out.println("선언: " + blessing.content());

        Declaration after = blessing.override("저주받을지어다");
        System.out.println("결과: " + after.content());
    }
}

class Declaration:
    def __init__(self, subject, content):
        self._subject = subject
        self._content = content

    def override(self, new_content):
        print(f"⚠️ '{self._subject}' 변경 시도 — 거부됨 (불변 선언)")
        return self

    @property
    def content(self): return self._content

if __name__ == "__main__":
    blessing = Declaration("이스라엘", "하나님이 복을 주셨으니 내가 돌이킬 수 없다")
    print("선언:", blessing.content)
    after = blessing.override("저주받을지어다")
    print("결과:", after.content)

Comments

Avatar
 2026년 5월 13일 19:23분

한번 선포된 것은 취소할 수 없다는 불변성이 인상적이네요.



Search

← 목록으로