[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
@@ -2,87 +2,224 @@
id: wiki-2026-0508-encapsulation-via-access-modifie
title: Encapsulation via Access Modifiers
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [P-Reinforce-AI-ACCESS-MODIFIERS]
aliases: [Access Modifiers, Visibility Modifiers, Encapsulation]
duplicate_of: none
source_trust_level: A
confidence_score: 1.0
tags: [OOP, Programming, AccessModifiers, Security]
confidence_score: 0.95
verification_status: applied
tags: [oop, encapsulation, access-modifier, language-design]
raw_sources: []
last_reinforced: 2026-04-20
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: unspecified
framework: unspecified
language: TypeScript
framework: General
---
# [[Encapsulation-via-Access-Modifiers|Encapsulation-via-Access-Modifiers]] (접근 제어자를 통한 캡슐화)
# Encapsulation via Access Modifiers
## 📌 한 줄 통찰 (The Karpathy Summary)
> "공개할 것인가, 숨길 것인가에 대한 명확한 선언." `public`, `private`, `protected`와 같은 키워드를 통해 클래스 멤버의 가시성을 제어하고 접근 경로를 설계하는 구체적인 실천 방식이다.
## 한 줄
> **"매 internal state 의 hide + boundary 의 explicit"**. 1967 Simula 의 OOP 의 도입 후 매 Bertrand Meyer 의 "Design by Contract", Parnas 의 information hiding 원칙 의 codify — 2026 modern lang 는 `public/private/protected` 의 보다, JS `#private`, Rust module visibility, TS `readonly`, Java `sealed`, C# `init`-only 의 fine-grained capability 의 dominate.
## 📖 구조화된 지식 (Synthesized Content)
- **Access Modifiers**:
- **Private**: 오직 해당 클래스 내부에서만 접근 가능. (철저한 보호)
- **Protected**: 해당 클래스와 이를 상속받은 자식 클래스에서 접근 가능.
- **Public**: 어디서든 접근 가능. (공개 API)
- **Default/Package-Private**: (언어마다 다름) 같은 패키지 내 공유.
- **Role**: 객체의 내부 상태를 외부로부터 고립시켜 '깨지기 쉬운 코드'가 되는 것을 방지함.
## 매 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- [[JavaScript|JavaScript]]/TypeScript 진영에서는 `#private` 문법이 표준화되기 전까지 접두사 `_`를 관습적으로 사용해왔다. 하지만 이는 강제성이 없어 '의도된 약속'에 의존했다면, 이제는 언어 차원의 강제성을 부여하는 것이 표준이다. 테스트 코드를 위해 `private`을 억지로 여는 것은 부적절한 설계 신호일 수 있다.
### 매 Access modifier 의 종류 (lang 별)
- **Java**: `public`, `protected`, package-private (default), `private`.
- **C#**: `public`, `protected`, `internal`, `private`, `protected internal`, `private protected`, `file`.
- **TypeScript**: `public` (default), `protected`, `private`, `#private` (true private — JS native).
- **Rust**: `pub`, `pub(crate)`, `pub(super)`, `pub(in path)`, default private to module.
- **Python**: convention only — `_underscore` (protected), `__double` (name-mangled).
- **Kotlin**: `public` (default), `internal` (module), `protected`, `private`.
## 🔗 지식 연결 (Graph)
- Related: [[Encapsulation-and-Information-Hiding|Encapsulation-and-Information-Hiding]] , [[Interface-Segregation-Principle|Interface-Segregation-Principle]]
- Language Specific: TypeScript-Private-Fields
### 매 Encapsulation 의 layer
- **Visibility**: who can see.
- **Mutability**: `final`, `readonly`, `const`, `val`.
- **Identity**: opaque type, newtype.
- **Invariant**: setter validation, factory.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 응용
1. API 의 stable surface 의 maintain (semver).
2. Library author 의 internal refactoring freedom.
3. Test isolation (private — black-box test 의 강제).
4. Security boundary (capability 의 leak 방지).
**언제 이 지식을 쓰는가:**
- *(TODO)*
## 💻 패턴
**언제 쓰면 안 되는가:**
- *(TODO)*
### TypeScript 의 `#private` (true private)
```typescript
class BankAccount {
#balance = 0; // 매 runtime 의 private — TS `private` 의 erase 의 X
readonly id: string;
## 🧪 검증 상태 (Validation)
constructor(id: string) { this.id = id; }
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
deposit(amount: number) {
if (amount <= 0) throw new Error('positive only');
this.#balance += amount;
}
get balance() { return this.#balance; }
}
## 🧬 중복 검사 (Duplicate Check)
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
## 🕓 변경 이력 (Changelog)
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
## 💻 코드 패턴 (Code Patterns)
**패턴 1:** *(TODO: 이 프로젝트 컨벤션 반영한 구조 스켈레톤)*
```text
# TODO
const a = new BankAccount('A1');
// a.#balance = 999; // SyntaxError 의 compile + runtime
```
## 🤔 의사결정 기준 (Decision Criteria)
### Rust 의 module visibility
```rust
mod auth {
pub struct User {
pub name: String,
password_hash: String, // 매 module 외부 의 invisible
}
**선택 A를 써야 할 때:**
- *(TODO)*
impl User {
pub fn new(name: &str, pw: &str) -> Self {
User { name: name.into(), password_hash: hash(pw) }
}
pub(crate) fn verify(&self, pw: &str) -> bool {
self.password_hash == hash(pw)
}
}
fn hash(s: &str) -> String { /* ... */ s.into() }
}
```
**선택 B를 써야 할 때:**
- *(TODO)*
### Java sealed + record
```java
public sealed interface Shape permits Circle, Square, Triangle {}
**기본값:**
> *(TODO)*
public record Circle(double radius) implements Shape {
public Circle { // 매 compact constructor 의 invariant
if (radius < 0) throw new IllegalArgumentException();
}
}
## ❌ 안티패턴 (Anti-Patterns)
// 매 exhaustive switch 의 enable
String describe(Shape s) {
return switch (s) {
case Circle c -> "circle r=" + c.radius();
case Square sq -> "square";
case Triangle t -> "tri";
};
}
```
- **[안티패턴]:** *(TODO: 무엇을 하면 안 되는가 + 이유 + 대신 무엇을)*
### C# init-only + required
```csharp
public class Order
{
public required string Id { get; init; } // 매 set 의 only at init
public DateTime Created { get; init; } = DateTime.UtcNow;
private decimal _total;
public decimal Total
{
get => _total;
private set => _total = value >= 0 ? value :
throw new ArgumentException();
}
}
var o = new Order { Id = "ORD-1" }; // 매 valid
// o.Id = "X"; // 매 error: init-only
```
### Python 의 convention + property
```python
class Vault:
def __init__(self, secret):
self._secret = secret # 매 protected (convention)
self.__truly_hidden = "obfuscated" # 매 _Vault__truly_hidden
@property
def secret(self):
# 매 read-only 의 access
return f"***{self._secret[-2:]}"
v = Vault("p@ssword")
print(v.secret) # ***rd
# v.secret = "x" # AttributeError
```
### Capability-based design (no class)
```typescript
// 매 closure-based 의 information hiding
function makeCounter() {
let count = 0;
return {
inc: () => ++count,
get: () => count,
// 매 reset 의 reveal 안 하면 의 capability 의 absent
};
}
const c = makeCounter();
c.inc(); c.inc();
console.log(c.get()); // 2 — count 의 access 의 X
```
### Friend / internal API (Kotlin)
```kotlin
// 매 internal 의 same module 만의 access
internal class IndexBuilder {
internal fun rebuild() { /* ... */ }
}
// 매 @PublishedApi 의 inline function 의 internal API 의 publish
@PublishedApi
internal fun secretImpl() = 42
inline fun publicWrapper() = secretImpl()
```
### Newtype 의 opaque ID
```rust
pub struct UserId(u64); // 매 tuple struct 의 inner 의 private
impl UserId {
pub fn new(id: u64) -> Self { UserId(id) }
pub fn value(&self) -> u64 { self.0 }
}
// 매 raw u64 의 mistakenly UserId 로 의 X — type-safe boundary
fn get_user(id: UserId) { /* ... */ }
// get_user(123); // compile error
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Library API | `pub` 의 minimal surface, internal 의 default |
| Domain model | private field + factory + invariant |
| DTO/value | `record` / `data class` / immutable |
| JS/TS browser code | `#private` (true private) 의 default |
| Cross-module refactoring | `internal`/`pub(crate)` 의 prefer |
**기본값**: most restrictive 의 default. 매 explicit 하 expand.
## 🔗 Graph
- 부모: [[Object-Oriented Programming]] · [[Information Hiding]]
- 변형: [[Module System]] · [[Capability-Based Security]]
- 응용: [[API Design]] · [[Library Design]] · [[Refactoring]]
- Adjacent: [[Abstraction]] · [[SOLID]] · [[Design by Contract]]
## 🤖 LLM 활용
**언제**: API surface review, accessor pattern 의 generate, visibility audit.
**언제 X**: language-specific subtle case (Kotlin internal mangling 등) 의 verify 필수.
## ❌ 안티패턴
- **Public field**: encapsulation 의 broken — invariant 의 enforce 의 X.
- **Getter/setter for everything**: encapsulation 의 illusion (Tell Don't Ask 위반).
- **Reflection 의 private 의 bypass**: test 의 implementation 의 lock.
- **Java private 의 inner class**: enclosing class 의 implicit access — confusion.
- **TS `private` 의 trust**: runtime 의 erase — `#private` 사용.
## 🧪 검증 / 중복
- Verified (Bloch "Effective Java" Item 15, Parnas 1972 "Decomposition", JLS, ECMA-262).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — TS/Rust/Java/C#/Kotlin/Python access patterns |