Advanced design patterns

From nlelouche/Unity-SkillForge · View interactive version

Advanced design patterns is an AI agent skill from the GitHub repository nlelouche/Unity-SkillForge. Principal Architect for SOLID & GoF Patterns in Unity 3D. Activate for complex systems, scalable mechanics, decoupled architectures, or refactoring God Classes.

How to use this skill

Skill file

Advanced Design Patterns

Overview

Transform high-level gameplay requirements into modular, testable, and maintainable systems. This skill enforces strict structural patterns (Strategy, Factory, Observer, Command) adapted for Unity's unique lifecycle.

When to Use

Available Patterns

| Pattern | Use Case | Template |

|---------|----------|----------|

| Strategy | Interchangeable behaviors (AI, attacks) | IStrategy.cs, StrategyContext.cs |

| Factory | Object creation with pooling | IFactory.cs, PooledFactory.cs |

| Observer | Decoupled event notifications | IObserver.cs |

| Command | Undo/redo, input recording | ICommand.cs, CommandInvoker.cs |

Thinking Process (Mandatory)

Before generating code, analyze:

  1. Identify the Axis of Change: What varies? (Behavior, Creation, Notification)
  2. Pattern Selection: Justify choice based on problem domain
  3. Unity Compatibility: MonoBehaviour vs ScriptableObject vs plain C#
  4. SOLID Validation: SRP and OCP compliance check

Procedure

  1. Generate Architecture Plan: Emit class diagram before implementation
  2. Define Contracts: Create interfaces with clear API boundaries
  3. Implement Concretes: Build decoupled implementations
  4. Inject Dependencies: Use VContainer or SO-based injection (no static Singletons!)
  5. Audit: Verify against constraints

Best Practices

Architecture Audit Checklist

Few-Shot Examples

Example 1: Strategy Pattern for AI

User: "I need different enemy AI behaviors like patrolling, chasing, and retreating."

Agent Output:

1. Generate Implementation Plan with Strategy Pattern
2. Create IEntityBehavior interface
3. Implement PatrolBehavior, ChaseBehavior, RetreatBehavior
4. Create EnemyController with SetBehavior(IBehavior) method
5. Inject behaviors via ScriptableObject references

Example 2: Factory with Pooling

User: "Create a projectile spawning system that doesn't cause GC spikes."

Agent Output:

1. Create IProjectileFactory interface
2. Implement ProjectilePooledFactory : PooledFactory<Projectile>
3. Configure initial pool size based on expected max concurrent
4. Add Return() method call on projectile destruction

Example 3: Observer for UI Updates

User: "Update the health bar when player takes damage without coupling."

Agent Output:

1. Create IHealthObserver interface
2. Create PlayerHealth : ISubject<HealthData>
3. Create HealthBarUI : IObserver<HealthData>
4. Subscribe in OnEnable, unsubscribe in OnDisable

TDD Contract

⚠️ Legacy Skill — Refactor Pending

Este skill NO tiene tests automatizados aún. El siguiente boilerplate es un punto de partida.

// Escribe estos tests ANTES de implementar:

// Test 1: should [expected behavior] when [condition]
[Test]
public void AdvancedDesignPatterns_Should{ExpectedBehavior}_When{Condition}()
{{
    // Arrange
    // TODO: Setup test fixtures
    
    // Act
    // TODO: Execute system under test
    
    // Assert
    Assert.Fail("Not implemented — write test first");
}}

// Test 2: should handle [edge case]
[Test]
public void AdvancedDesignPatterns_ShouldHandle{EdgeCase}()
{{
    // Arrange
    // TODO: Setup edge case scenario
    
    // Act
    // TODO: Execute
    
    // Assert
    Assert.Fail("Not implemented");
}}

// Test 3: should throw when [invalid input]
[Test]
public void AdvancedDesignPatterns_ShouldThrow_When{InvalidInput}()
{{
    // Arrange
    var invalidInput = default;
    
    // Act & Assert
    Assert.Throws<Exception>(() => {{ /* execute */ }});
}}

Pasos para completar el TDD:

  1. Descomenta los tests above
  2. Implementa la funcionalidad mínima para que compile
  3. Ejecuta los tests — deben fallar (RED)
  4. Implementa la funcionalidad real
  5. Verifica que los tests pasen (GREEN)
  6. Refactorea manteniendo los tests verdes

Nota: Este skill fue marcado como tdd_first: false durante la auditoría v2.0.1. La sección TDD fue agregada automáticamente pero requiere customización manual para reflejar el comportamiento real del skill.

Related Skills

Generator Script

python scripts/pattern_gen.py --pattern strategy --name EnemyAI --namespace Game.AI
python scripts/pattern_gen.py --pattern factory --name Projectile --namespace Game.Combat
Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.