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

liskov-substitution-principle里氏替换原则

Agent Skill

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

总安装

570

周安装

24

GitHub Stars

10

下载量

45
CodexClaudeCursorGemini CLI

安装说明

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

GitHub

来源数

3

许可证

MIT

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

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

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

命令行安装

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

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

简介

liskov-substitution-principle 用于辅助前端页面、组件、样式和交互逻辑的开发与维护。

  • 适合生成或审查 React、Next.js、Vue、Tailwind、CSS 等相关代码。
  • 需结合项目现有设计系统和构建方式使用,避免生成孤立片段。
  • 涉及页面改动时应配合本地预览和构建检查确认视觉效果。
  • 适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。

SKILL.md

Liskov Substitution Principle (LSP)

Overview

Subtypes must be substitutable for their base types without altering program correctness.

If S is a subtype of T, objects of type T can be replaced with objects of type S without breaking the program. Subclasses must honor the contracts of their parent classes.

When to Use

  • Creating a class that extends another class
  • Overriding methods from a parent class
  • Implementing an interface
  • Feeling like you need to throw exceptions in overridden methods
  • Inheritance hierarchy feels "forced"

The Iron Rule

NEVER create a subclass that breaks the expectations of the parent class.

No exceptions:

  • Not for "it's the standard approach"
  • Not for "I'll note it as an anti-pattern"
  • Not for "the requirements say to extend"
  • Not throwing exceptions in overridden methods
  • Not making overridden methods no-ops

Providing violating code "with a caveat" is still providing violating code.

Detection: The Substitution Test

Ask: "Can I replace every instance of Parent with Child without breaking anything?"

function processRectangle(rect: Rectangle): void {
  rect.setWidth(5);
  rect.setHeight(10);
  assert(rect.getArea() === 50); // Always true for Rectangle
}

// If Square extends Rectangle:
const square = new Square(5);
processRectangle(square); // FAILS! Area is 100, not 50

If substitution breaks code, you have an LSP violation.

Detection: Override Smells

These overrides indicate LSP violations:

// ❌ VIOLATION: Throwing in override
class Penguin extends Bird {
  fly(): void {
    throw new Error("Penguins can't fly"); // Breaks callers expecting fly()
  }
}

// ❌ VIOLATION: No-op override
class ReadOnlyStorage extends FileStorage {
  write(path: string, content: string): void {
    // Silently does nothing - breaks caller expectations
  }
}

// ❌ VIOLATION: Changing behavior semantics
class Square extends Rectangle {
  setWidth(w: number): void {
    this.width = w;
    this.height = w; // Changes height too - breaks expectations
  }
}

The Correct Pattern: Composition & Interfaces

Don't force inheritance. Use interfaces to define capabilities.

Square/Rectangle Problem

// ✅ CORRECT: Separate types, shared interface
interface Shape {
  getArea(): number;
}

class Rectangle implements Shape {
  constructor(private width: number, private height: number) {}
  getArea(): number { return this.width * this.height; }
  setWidth(w: number): void { this.width = w; }
  setHeight(h: number): void { this.height = h; }
}

class Square implements Shape {
  constructor(private size: number) {}
  getArea(): number { return this.size * this.size; }
  setSize(s: number): void { this.size = s; }
}

Bird/Penguin Problem

// ✅ CORRECT: Capability interfaces
interface Flyable {
  fly(): void;
}

abstract class Bird {
  abstract eat(): void;
}

class Sparrow extends Bird implements Flyable {
  eat(): void { /* ... */ }
  fly(): void { /* ... */ }
}

class Penguin extends Bird {
  eat(): void { /* ... */ }
  swim(): void { /* ... */ }
  // No fly() - doesn't promise what it can't deliver
}

ReadOnly Problem

// ✅ CORRECT: Separate interfaces
interface Readable {
  read(path: string): string;
}

interface Writable {
  write(path: string, content: string): void;
  delete(path: string): void;
}

class FileStorage implements Readable, Writable {
  read(path: string): string { /* ... */ }
  write(path: string, content: string): void { /* ... */ }
  delete(path: string): void { /* ... */ }
}

class AuditLogStorage implements Readable {
  read(path: string): string { /* ... */ }
  // No write/delete - doesn't extend something it can't honor
}

Pressure Resistance Protocol

1. "Just Override and Throw"

Pressure: "Handle the fact they can't fly by throwing an error"

Response: Throwing in an override violates the contract. Code expecting fly() will crash.

Action: Restructure with interfaces. Don't inherit methods you can't honor.

2. "It's the Standard Approach"

Pressure: "Override-and-throw is the standard way to do this"

Response: "Standard" doesn't mean correct. This pattern causes runtime failures.

Action: Use composition and interfaces instead.

3. "The Requirements Say Extend"

Pressure: "Square must extend Rectangle per the requirements"

Response: Requirements that mandate LSP violations are wrong. Push back.

Action:

"A Square extending Rectangle violates LSP and will cause bugs.
I recommend: [correct approach with interfaces].
Should I implement the correct structure, or document this as known tech debt?"

4. "I'll Note It's an Anti-Pattern"

Pressure: Internal rationalization

Response: Providing bad code with a caveat is still providing bad code.

Action: Provide only the correct solution. Don't implement the violation.

Red Flags - STOP and Reconsider

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

  • Overriding a method to throw an exception
  • Overriding a method to do nothing (no-op)
  • Overriding a method to change its fundamental behavior
  • Subclass can't do everything the parent can
  • Inheritance feels forced or unnatural
  • Using instanceof checks to handle subtypes differently

All of these mean: Use composition and interfaces instead.

Quick Reference

ViolationCorrect Approach
Square extends RectangleBoth implement Shape interface
Penguin extends Bird (with fly)Bird base + Flyable interface
ReadOnlyStorage extends StorageSeparate Readable/Writable interfaces
Child throws in overrideChild shouldn't extend that parent
Child no-ops an overrideChild shouldn't extend that parent

Common Rationalizations (All Invalid)

ExcuseReality
"It's the standard approach"Common doesn't mean correct.
"I provided a caveat"Bad code with warnings is still bad code.
"Requirements say extend"Requirements can be wrong. Push back.
"Throwing makes it explicit"Throwing breaks callers. Compile errors are better.
"No-op is safe"Silent failures hide bugs.
"It's just for this one case"One violation leads to more. Fix it properly.

The Bottom Line

If a subclass can't fully substitute for its parent, don't use inheritance.

Use interfaces to define capabilities. Use composition to share behavior. Never override methods with exceptions or no-ops.

When asked to create violating inheritance: restructure with interfaces instead. Don't provide the violation "with a caveat."

适合场景

01

用户想查找某类 Agent Skill 时

02

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

03

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

04

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

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

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

能力 4

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

能力 5

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

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

平台分布

Claude Code

26.37%
按下载量换算12

Codex

21.27%
按下载量换算10

windsurf

17.44%
按下载量换算8

Antigravity

12.71%
按下载量换算6

trae

8.11%
按下载量换算4

OpenCode

3.18%
按下载量换算1

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

只读

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

安装前确认

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

来源信息

继续浏览同类 Skills