Token导航 LogoToken导航TokenDH.com
前端设计只读github未标认证来源可访问clear审计通过

open-closed-principle开闭原则

Agent Skill

用于辅助前端页面、组件、样式和交互逻辑的开发与维护。它适合让 Agent 生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码,整理组件结构,或定位布局和性能问题。使用时需要结合项目现有设计系统、路由和构建方式,避免只生成孤立片段;涉及页面改动时,应配合本地预览和构建检查确认视觉效果。

总安装

624

周安装

25

GitHub Stars

10

下载量

202
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:open-closed-principle(开闭原则)
来源仓库:https://github.com/yanko-belov/code-craft
仓库路径:skills/open-closed-principle
安装命令:
npx skills add https://github.com/yanko-belov/code-craft --skill open-closed-principle
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/yanko-belov/code-craft --skill open-closed-principle

简介

开闭原则用于辅助前端组件和样式开发。

  • 适合生成 React、Vue 或 Tailwind 代码。
  • 使用时需结合现有设计系统和路由。适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。
  • 避免只生成孤立片段代码。open-closed-principle 属于前端设计类 Skill,可作为该场景下的辅助能力补充。
  • 涉及页面改动时应配合预览检查效果。

SKILL.md

Open/Closed Principle (OCP)

Overview

Software entities should be open for extension, but closed for modification.

When new functionality is needed, extend the system with new code rather than modifying existing code. If adding a feature requires changing existing if/else chains, you're violating OCP.

When to Use

  • Adding a new payment method, notification channel, export format, etc.
  • Tempted to add another if/else or switch case
  • Existing code works but needs new variants
  • Feature request: "add support for X"

The Iron Rule

NEVER add another branch to an existing if/else or switch statement.

No exceptions:

  • Not for "it's just one more case"
  • Not for "we'll refactor later"
  • Not for "the pattern is already established"
  • Not for "it's faster this way"

Noting the problem while doing it anyway is still a violation.

Detection: The "Add Branch" Smell

If your solution involves this pattern, STOP:

// ❌ VIOLATION: Adding branches
if (type === 'existing') {
  // existing logic
} else if (type === 'new') {  // ← Adding this = OCP violation
  // new logic
}

Every else if you add is a modification to existing code that works.

The Correct Pattern: Strategy/Plugin

Instead of modifying, extend:

// ✅ CORRECT: Define interface, implement separately
interface PaymentMethod {
  process(amount: number): boolean;
}

class CreditCardPayment implements PaymentMethod {
  process(amount: number): boolean { /* ... */ }
}

class PayPalPayment implements PaymentMethod {
  process(amount: number): boolean { /* ... */ }
}

// Processor doesn't change when adding new methods
class PaymentProcessor {
  constructor(private methods: Map<string, PaymentMethod>) {}

  process(type: string, amount: number): boolean {
    const method = this.methods.get(type);
    if (!method) throw new Error(`Unknown: ${type}`);
    return method.process(amount);
  }

  register(type: string, method: PaymentMethod): void {
    this.methods.set(type, method);
  }
}

Adding Apple Pay? Create ApplePayPayment, call register(). Zero modifications to PaymentProcessor.

Pressure Resistance Protocol

1. "Just Add Another Case"

Pressure: "The quickest approach: add more if/else branches"

Response: Adding branches takes the same time as creating a new class. The "quick" approach creates unmaintainable code.

Action: Create interface + implementation. Register the new variant.

2. "The Pattern Is Already There"

Pressure: "The code already uses if/else, just extend it"

Response: Existing violations don't justify more violations. This is the moment to refactor.

Action:

  1. Extract interface from existing branches
  2. Convert branches to implementations
  3. Add your new implementation

3. "We'll Refactor Later"

Pressure: "Add it now, we'll clean up later"

Response: You won't. The if/else will grow to 15 cases. Technical debt compounds.

Action: Refactor now. It takes 10 minutes. Adding to the mess takes the same time.

4. "I'll Note It But Do It Anyway"

Pressure: Internal rationalization that awareness = compliance

Response: Noting the problem while violating the principle is still a violation.

Action: Don't add the branch. Refactor instead. Comments about "should use strategy pattern" are not acceptable.

Red Flags - STOP and Reconsider

If you notice ANY of these, you're about to violate OCP:

  • Adding else if to existing conditional
  • Adding case to existing switch
  • Typing the same if/else structure that's already there
  • Thinking "I'll mention this should be refactored"
  • Method has more than 3 type-based branches

All of these mean: Create an interface and implementation instead.

Refactoring Existing Violations

When you encounter existing if/else chains:

// BEFORE: 5 branches in processPayment
if (type === 'card') { ... }
else if (type === 'paypal') { ... }
else if (type === 'apple') { ... }
else if (type === 'google') { ... }
else if (type === 'crypto') { ... }

// AFTER: Strategy pattern
interface PaymentMethod { process(amount: number): boolean; }
class CardPayment implements PaymentMethod { ... }
class PayPalPayment implements PaymentMethod { ... }
// etc.

Refactor on touch: When asked to add the 6th branch, refactor instead.

Quick Reference

SituationWrongRight
Add payment methodAdd else if (type === 'new')Create NewPayment implements PaymentMethod
Add notification channelAdd else if (channel === 'slack')Create SlackNotifier implements Notifier
Add export formatAdd case 'xlsx':Create XlsxExporter implements Exporter
Add discount typeAdd else if (discount === 'bogo')Create BogoDiscount implements DiscountStrategy

Common Rationalizations (All Invalid)

ExcuseReality
"It's just one more case"That's what they said about the previous 5 cases.
"I noted it should be refactored"Notes don't count. Refactor or don't, but don't pretend awareness is action.
"The code already uses if/else"Existing violations don't justify more violations.
"Strategy pattern is overkill"3+ branches = strategy pattern is correct engineering.
"It's faster to add a branch"It's not. You type the same code either way.
"We can refactor later"You won't. The branch count will double.

The Bottom Line

Open for extension. Closed for modification.

When adding new functionality:

  1. Create an interface (if none exists)
  2. Implement the interface for the new variant
  3. Register/inject the new implementation

Never add branches. Never "note it but do it anyway." Awareness without action is not compliance.

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

04

需要参考平台分布和安装热度时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

补充不同宿主或平台的使用分布数据

能力 5

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

29.34%
按下载量换算59

Claude Code

23.04%
按下载量换算47

windsurf

17.98%
按下载量换算36

Antigravity

13.21%
按下载量换算27

trae

8.9%
按下载量换算18

OpenCode

3.48%
按下载量换算7

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

该 Skill 主要提供规则、说明或参考内容,本身偏只读;真正读写文件、联网或执行命令仍取决于宿主 Agent 的任务。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

来源信息

继续浏览同类 Skills