microwind/ai-skills/encapsulation-principle
Claude Code and agent skill by microwind/ai-skills
Trust tier 3
- community
Source check pending
Checking that the SKILL.md file is still in the repository.
隐藏对象的内部实现细节,只通过公共接口暴露必要的操作,保护数据完整性。
Install
npx vskill@latest install microwind/ai-skills/encapsulation-principleSKILL.md
封装原则 (Encapsulation Principle)
概述
封装是面向对象设计的基石:隐藏对象内部状态和实现细节,只通过受控的接口与外部交互。
核心思想:
- 数据和操作数据的方法绑定在一起
- 内部实现对外不可见
- 通过公共方法控制访问和修改
- 保护对象不变量不被外部破坏
代码示例
封装层次
| 层次 | 说明 | 示例 | |
|---|---|---|---|
| 字段封装 | 私有字段 + getter/setter | private + getXxx() | |
| 行为封装 | 数据和行为在一起 | account.withdraw() | |
| 实现封装 | 隐藏算法细节 | sort() 不暴露排序算法 | |
| 类型封装 | 隐藏具体类型 | 返回接口而非实现类 | |
| 模块封装 | 隐藏内部类 | 只导出公共 API |
常见封装破坏
最佳实践
- 默认 private - 字段和方法默认私有,按需开放
- 不可变优先 - 能用 final/readonly 就用
- 防御性拷贝 - 返回集合或可变对象时返回副本
- 告诉不要问 - 让对象执行操作,而非获取数据后外部操作
与其他原则的关系
总结
封装核心:隐藏实现,暴露接口,保护不变量。
实践:字段私有、行为内聚、返回不可变视图、用类型代替裸数据。
Security
- Security scanPassed, 100 of 100Pattern checks on the skill's files.
- Model reviewPassed
Quality evaluation
- Task evaluationNot runNo evaluation yet. Run one in Skill Studio.
Source
- SKILL.md in the repositoryCheckingdesign-principles/encapsulation-principle/SKILL.md
Popularity
- Repository stars
- 75
- Repository forks
- 19
- 7-day trend score
- 22
Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 2 Sep 2026, a month ago.
Works with
Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.
- Claude Code
- Cursor
- GitHub Copilot
- Windsurf
- Codex
- Gemini CLI
- Cline
- Amp
- Roo Code
- Goose
- Continue
- Kilo Code
- Junie
- Trae
- OpenHands
- Qwen Code
- AI Agent
- Tabnine Chat
- Sourcegraph Cody
- Amazon Q
- Firebase Genkit
- Sweep
- Devin
- Phind
- Mentat
- Claude Desktop
- Aider
- GPT Pilot
- Plandex
- bolt.new
- v0
- Replit Agent
- Anse
- TypingMind
- BetterChatGPT
- LibreChat
- Warp AI
- Pieces
- Fig AI
Embed badge
[](https://verified-skill.com/skills/microwind/ai-skills/encapsulation-principle)