Intelligently identify design patterns, accurately recommend solutions, professionally optimize code structure, systematically review architecture design
Trigger Conditions
This skill is triggered when the user has the following needs:
- Analyze which design patterns are used in the code
- Select appropriate design patterns for a specific scenario
- Optimize the design structure of existing code
- Review the reasonableness of the system architecture
- Identify design smells in the code
- Compare the pros and cons of multiple design patterns
Atomic Capability List
This skill provides 8 atomic capabilities, which can be invoked independently or in combination:
1. analyze-pattern-usage (Analyze Pattern Usage)
Description: Analyze the design patterns already used in the code and their correctness
Input:
{
"capability": "analyze-pattern-usage",
"context": {
"code": "string - 代码片段或文件路径",
"language": "string - 编程语言",
"framework": "string - 框架(可选)"
},
"parameters": {
"depth": "basic|detailed - 分析深度",
"include_examples": "boolean - 是否包含示例"
}
}
Output:
{
"status": "success|error",
"result": {
"patterns_found": [
{
"name": "string - 模式名称",
"type": "creational|structural|behavioral",
"location": "string - 代码位置",
"correctness": "correct|partial|incorrect",
"assessment": "string - 评估说明"
}
],
"summary": "string - 分析总结"
},
"metadata": {
"execution_time_ms": 1200,
"confidence_score": 0.95
}
}
Example:
输入:分析 UserService.java 中的设计模式
输出:识别到单例模式(正确)、工厂方法(部分正确,缺少抽象产品)
2. detect-pattern-misuse (Detect Pattern Misuse)
Description: Detect improper use or over-engineering of design patterns
Input:
{
"capability": "detect-pattern-misuse",
"context": {
"code": "string - 代码片段",
"pattern_name": "string - 特定模式(可选)"
},
"parameters": {
"strictness": "low|medium|high - 检测严格度"
}
}
Output:
{
"status": "success|error",
"result": {
"misuses": [
{
"pattern": "string - 模式名称",
"issue": "string - 问题描述",
"severity": "low|medium|high",
"suggestion": "string - 改进建议"
}
]
},
"metadata": {
"execution_time_ms": 800,
"confidence_score": 0.88
}
}
Example:
输入:检测 PaymentProcessor 中的单例模式使用
输出:高风险 - 单例持有可变状态,建议改为无状态实现或使用依赖注入
3. recommend-pattern (Recommend Design Pattern)
Description: Recommend the most suitable design pattern based on the scenario
Input:
{
"capability": "recommend-pattern",
"context": {
"scenario": "string - 业务场景描述",
"constraints": ["string - 约束条件"],
"existing_code": "string - 现有代码(可选)"
},
"parameters": {
"max_recommendations": "number - 最大推荐数",
"include_alternatives": "boolean - 是否包含替代方案"
}
}
Output:
{
"status": "success|error",
"result": {
"recommendations": [
{
"pattern": "string - 模式名称",
"priority": 1,
"rationale": "string - 推荐理由",
"applicability": "high|medium|low",
"trade_offs": ["string - 权衡点"]
}
]
},
"metadata": {
"execution_time_ms": 1500,
"confidence_score": 0.90
}
}
Example:
输入:需要支持多种支付方式,且未来可能扩展
输出:推荐策略模式(高优先级)+ 工厂方法(中优先级)
4. compare-patterns (Compare Multiple Patterns)
Description: Compare the pros, cons, and applicability of multiple design patterns
Input:
{
"capability": "compare-patterns",
"context": {
"patterns": ["string - 模式名称列表"],
"scenario": "string - 应用场景"
},
"parameters": {
"criteria": ["complexity", "flexibility", "performance"]
}
}
Output:
{
"status": "success|error",
"result": {
"comparison": {
"table": "对比表格数据",
"winner": "string - 最佳模式",
"analysis": "string - 详细分析"
}
},
"metadata": {
"execution_time_ms": 1000,
"confidence_score": 0.92
}
}
Example:
输入:对比策略模式和状态模式在订单状态管理中的适用性
输出:策略模式更适合(状态转换简单,行为变化多)
5. suggest-refactoring (Suggest Refactoring Plan)
Description: Provide specific code refactoring suggestions
Input:
{
"capability": "suggest-refactoring",
"context": {
"code": "string - 待重构代码",
"target_pattern": "string - 目标模式(可选)"
},
"parameters": {
"goal": "maintainability|extensibility|performance",
"preserve_behavior": true
}
}
Output:
{
"status": "success|error",
"result": {
"refactoring_plan": [
{
"step": 1,
"action": "string - 重构动作",
"before": "string - 重构前代码",
"after": "string - 重构后代码",
"benefits": ["string - 收益说明"]
}
]
},
"metadata": {
"execution_time_ms": 2000,
"confidence_score": 0.87
}
}
Example:
输入:优化包含大量if-else的订单处理代码
输出:建议重构为策略模式,提供分步重构方案
6. generate-code-example (Generate Code Example)
Description: Generate a code example for the specified design pattern
Input:
{
"capability": "generate-code-example",
"context": {
"pattern": "string - 模式名称",
"language": "string - 编程语言",
"domain": "string - 业务领域(可选)"
},
"parameters": {
"complexity": "simple|medium|complete",
"include_comments": true
}
}
Output:
{
"status": "success|error",
"result": {
"code_example": "string - 完整代码示例",
"explanation": "string - 代码说明",
"key_points": ["string - 关键点"]
},
"metadata": {
"execution_time_ms": 1800,
"confidence_score": 0.95
}
}
Example:
输入:生成Java的观察者模式示例,用于订单状态通知
输出:提供完整的OrderSubject、OrderObserver实现及使用示例
7. evaluate-architecture (Evaluate Architecture Design)
Description: Evaluate the reasonableness and design quality of the system architecture
Input:
{
"capability": "evaluate-architecture",
"context": {
"architecture_description": "string - 架构描述",
"key_modules": ["string - 关键模块"],
"design_goals": ["string - 设计目标"]
},
"parameters": {
"depth": "high-level|detailed",
"focus_areas": ["coupling", "cohesion", "scalability"]
}
}
Output:
{
"status": "success|error",
"result": {
"assessment": {
"overall_score": 0.85,
"strengths": ["string - 优势"],
"weaknesses": ["string - 劣势"],
"risks": ["string - 风险"]
},
"recommendations": ["string - 改进建议"]
},
"metadata": {
"execution_time_ms": 3000,
"confidence_score": 0.85
}
}
Example:
输入:评估微服务架构的订单系统
输出:整体评分85分,指出服务间耦合过高,建议使用外观模式简化调用
8. identify-design-smell (Identify Design Smell)
Description: Identify design smells and potential issues in the code
Input:
{
"capability": "identify-design-smell",
"context": {
"code": "string - 代码片段",
"code_metrics": "object - 代码指标(可选)"
},
"parameters": {
"smell_types": ["all"],
"threshold": "low|medium|high"
}
}
Output:
{
"status": "success|error",
"result": {
"smells": [
{
"type": "string - 异味类型",
"location": "string - 代码位置",
"description": "string - 问题描述",
"severity": "low|medium|high",
"refactoring": "string - 重构建议"
}
]
},
"metadata": {
"execution_time_ms": 1200,
"confidence_score": 0.89
}
}
Example:
输入:分析UserManager类的设计异味
输出:识别到上帝类(高严重)、紧耦合(中严重),建议拆分为多个职责单一的类
Composite Capability Combinations
Atomic capabilities can be combined into 4 composite capabilities, corresponding to common usage scenarios:
pattern-recognition (Pattern Recognition)
Combination:analyze-pattern-usage + detect-pattern-misuse
Applicable Scenarios:
- Identify design patterns during code review
- Learn the application of design patterns in projects
- Evaluate the correctness of pattern usage
Invocation Example:
分析这段代码使用了哪些设计模式,是否有误用?
pattern-recommendation (Pattern Recommendation)
Combination:recommend-pattern + compare-patterns
Applicable Scenarios:
- Select design patterns for new feature development
- Compare the pros and cons of multiple patterns
- Determine the optimal design solution
Invocation example:
我需要实现一个支持多种通知方式的功能,推荐合适的设计模式
code-optimization (code optimization)
Combination:suggest-refactoring + generate-code-example
Applicable scenarios:
- Refactor legacy code
- Optimize complex conditional logic
- Extract duplicate code
Invocation example:
优化这段包含大量if-else的代码,并给出重构后的示例
architecture-review (architecture review)
Combination:evaluate-architecture + identify-design-smell
Applicable scenarios:
- System architecture assessment
- Identify design issues at the architectural level
- Improve the overall system design
Invocation example:
审查这个微服务架构设计,识别潜在问题
Error handling
Error code definitions
| Error code | Message | Fallback strategy |
|---|---|---|
| INVALID_CODE_FORMAT | The provided code format cannot be parsed | Please provide a valid code snippet or file path |
| PATTERN_NOT_FOUND | No known design pattern was recognized | Try using a more complete code example, or manually describe the design intent |
| AMBIGUOUS_SCENARIO | The scenario description is not clear enough to determine a recommendation | Please provide more context, such as business goals and constraints |
| ARCHITECTURE_TOO_COMPLEX | The architecture is too complex to complete a comprehensive review | It is recommended to review module by module, or provide an architecture diagram to assist the analysis |
| UNSUPPORTED_LANGUAGE | Unsupported programming language | Please use mainstream languages such as Java, Python, and JavaScript |
| INSUFFICIENT_CONTEXT | Insufficient context information | Please provide more code context or project background |
Fallback strategy
When an error is encountered, execute the fallback in the following priority order:
-
Ask the user to provide more information
- Explain the specific information that needs to be supplemented
- Provide an information template or example
-
Narrow the analysis scope
- Suggest that the user specify a particular module or function
- Process complex code in segments
-
Provide general advice instead of a specific solution
- Give directional advice based on design principles
- Recommend relevant design pattern documentation
-
Recommend manual review
- When automated analysis cannot meet the requirements
- Suggest consulting an architect or senior developer
Usage examples
Example 1: Identify design patterns in code
User input:
分析这个数据库连接池类的设计模式使用
Skill execution:
- Call analyze-pattern-usage to analyze the code
- Call detect-pattern-misuse to detect potential issues
Output result:
识别到的设计模式:
✓ 单例模式 - 正确实现,使用双重检查锁定
✓ 工厂方法 - 部分正确,缺少抽象产品接口
⚠ 潜在问题:单例持有可变状态,建议改为无状态实现
Example 2: Recommend design patterns
User input:
我需要实现一个支持多种支付方式的系统,推荐合适的设计模式
Skill execution:
- Call recommend-pattern to get a recommendation list
- Call compare-patterns to compare candidate patterns
Output result:
推荐方案:
1. 策略模式(高优先级)- 封装不同支付算法
2. 工厂方法(中优先级)- 创建具体支付策略
3. 外观模式(低优先级)- 简化支付流程调用
对比分析:策略模式 vs 状态模式
- 策略模式更适合:支付方式间无状态转换
- 实现更简单,扩展性更好
Example 3: Code optimization
User input:
优化这段订单处理代码,它有很多if-else判断
Skill execution:
- Call suggest-refactoring to generate a refactoring plan
- Call generate-code-example to generate example code
Output result:
重构建议:将条件判断重构为策略模式
步骤1:定义策略接口
步骤2:提取各支付方式为具体策略
步骤3:修改上下文类使用策略
重构收益:
- 代码复杂度降低60%
- 新增支付方式无需修改现有代码
- 符合开闭原则
Example 4: Architecture review
User input:
审查这个微服务架构的设计
Skill execution:
- Call evaluate-architecture to evaluate the overall architecture
- Call identify-design-smell to identify design smells
Output Results:
架构评估报告:
整体评分:82/100
优势:
- 服务职责划分清晰
- 使用API网关统一入口
风险:
- 服务间直接调用过多,建议引入外观模式
- 缺少熔断机制,建议使用断路器模式
改进建议:
1. 封装外部服务调用为外观接口
2. 添加Hystrix实现服务熔断
3. 考虑使用事件驱动减少同步调用
Notes
Usage Limitations
-
Code Length Limit
- A single analysis should not exceed 1000 lines of code
- For very large files, it is recommended to analyze them by module
-
Language Support
- Main support: Java, Python, JavaScript/TypeScript, C#, Go
- Analysis accuracy may be reduced for other languages
-
Architecture Complexity
- For very large systems, it is recommended to review them by layer
- Key modules need to be provided rather than all code
Best Practices
-
Provide Complete Context
- Include related classes and methods
- Explain the business background and goals
-
Clarify Optimization Goals
- Maintainability, extensibility, performance
- Different goals may lead to different recommended solutions
-
Iterative Optimization
- Identify problems first, then optimize step by step
- Avoid large-scale refactoring all at once
-
Validate Recommendations
- Automated analysis is for reference only
- Key decisions require manual confirmation
Design Pattern Knowledge Base
This skill is based on the following 23 classic design patterns:
Creational Patterns (5)
- Singleton Pattern - A globally unique instance
- Factory Method - Defer instantiation to subclasses
- Abstract Factory - Create families of related objects
- Builder Pattern - Build complex objects step by step
- Prototype Pattern - Create objects by copying
Structural Patterns (7)
- Adapter Pattern - Interface conversion
- Bridge Pattern - Separation of abstraction and implementation
- Composite Pattern - Tree structure
- Decorator Pattern - Dynamically add responsibilities
- Facade Pattern - Simplify the interface
- Flyweight Pattern - Share fine-grained objects
- Proxy Pattern - Control object access
Behavioral Patterns (11)
- Chain of Responsibility Pattern - Multiple objects handle a request
- Command Pattern - Parameterize requests
- Interpreter Pattern - Language interpretation
- Iterator Pattern - Sequentially access a collection
- Mediator Pattern - Encapsulate object interactions
- Memento Pattern - Save and restore state
- Observer Pattern - One-to-many dependency notification
- State Pattern - State determines behavior
- Strategy Pattern - Encapsulate a family of algorithms
- Template Method Pattern - Algorithm skeleton
- Visitor Pattern - Operations performed on elements
Version History
v1.1.0 (2026-03-01)
Optimization Content:
- Apply the skill-manager optimization strategy
- Atomically split into 8 atomic capabilities
- Define 4 composite capability combinations
- Standardized input/output interfaces
- Improve the error handling mechanism
- Add metadata enhancements (use_cases, reliability_score, etc.)
v1.0.0 (2026-03-01)
Initial version:
- Integrate a knowledge base of 23 classic design patterns
- Provide four core capabilities: identification, recommendation, optimization, and review
- Support the complete workflow
Start using the Design Pattern Advisor to improve the quality of your code design!