ARCHITECTURE_HANDBOOK
Architecture Handbook
System-Based Design for Tower Defense Game
📚 Table of Contents
1. Introduction
1.1 Purpose of This Document
1.2 Project Context
1.3 Architectural Philosophy
Core Principles
Design Goals
2. Core Architectural Concepts
2.1 System-Based Architecture Overview
The Problem: Monolithic Update() Pattern
The Solution: System-Based Architecture
Key Differences
Aspect
MonoBehaviour Pattern
System-Based Pattern
2.2 The Three Pillars
Pillar 1: Entities (Data Providers)
Pillar 2: Systems (Logic Processors)
Pillar 3: Manager (Orchestrator)
2.3 Data Flow
Execution Flow Diagram
2.4 Memory Layout
Cache-Friendly Iteration
3. Design Patterns
3.1 System Pattern (Custom ECS-like)
Intent
Structure
Implementation
Benefits
Applicability
3.2 Observer Pattern (Event-Driven)
Intent
Structure
Implementation
Benefits
Critical Notes
3.3 Object Pool Pattern
Intent
Structure
Implementation
Critical: OnEnable vs Start
Performance Impact
3.4 Strategy Pattern (Targeting)
Intent
Structure
Implementation
Benefits
Performance Note
3.5 Singleton Pattern (Managed)
Intent
Structure
Implementation
Benefits
Anti-Pattern Warning
4. Interface System
4.1 Interface Hierarchy
4.2 System Interfaces
IGameSystem
4.3 Entity Interfaces
IMoveable
IAttacker
ITargetable
IDamageable
IProjectile
IEffect
4.4 Utility Interfaces
IPoolable
ITargetingStrategy
4.5 Composition Example
5. System Implementations
5.1 MovementSystem
Purpose
Responsibilities
Architecture
Full Implementation
Performance Metrics
Usage Example
5.2 AttackSystem
Purpose
Responsibilities
Architecture
Full Implementation
Performance Metrics
Targeting Strategy Example
Usage Example
5.3 ProjectileSystem
Purpose
Responsibilities
Architecture
Full Implementation
Performance Metrics
Usage Example
5.4 EffectSystem
Purpose
Responsibilities
Architecture
Full Implementation
Performance Metrics
Usage Example
6. Entity Design
6.1 Entity Principles
Entities Are Data Providers
Registration Pattern
6.2 Enemy Entity
Complete Implementation
Enemy Variants
6.3 Tower Entity
Complete Implementation
Tower Variants
7. Performance Principles
7.1 Batch Processing
Core Concept
Why It's Faster
Performance Impact
7.2 Cache Optimization
CPU Cache Hierarchy
Sequential vs Random Access
Data Locality
Array of Structures (AoS) vs Structure of Arrays (SoA)
7.3 Avoiding LINQ in Hot Paths
Why LINQ Is Slow
Manual Iteration Instead
When LINQ Is Acceptable
7.4 Coroutines for Infrequent Tasks
Problem: FixedUpdate for Non-Physics Tasks
Solution: Coroutine with Delay
Performance Impact
When to Use Coroutines
8. Best Practices
8.1 System Design Guidelines
1. Single Responsibility Principle
2. Clear Priority Order
3. Batch Processing Always
4. Interface-Based Design
8.2 Entity Design Guidelines
1. No Update() in Entities
2. OnEnable/OnDisable Registration
3. Interface Composition
4. Minimal Public API
8.3 Performance Guidelines
1. Profile Before Optimizing
2. Avoid Premature Optimization
3. Cache Component References
4. Use Object Pooling
5. Avoid Allocations in Hot Paths
8.4 Testing Guidelines
1. Unit Test Systems in Isolation
2. Integration Test System Interactions
3. Performance Test with Profiler
9. Migration Guide
9.1 From Monolithic to System-Based
Step 1: Identify Systems
Step 2: Create Interfaces
Step 3: Implement Systems
Step 4: Refactor Entities
Step 5: Create GameSystemsManager
Step 6: Test & Validate
9.2 Common Migration Pitfalls
Pitfall 1: Forgetting Unregistration
Pitfall 2: Using Start Instead of OnEnable
Pitfall 3: Wrong System Priority
Pitfall 4: Keeping Update() Methods
10. Appendix
10.1 Glossary
10.2 Performance Benchmarks
Test Environment
Results
Metric
Before Refactor
After Refactor
Improvement
Load Testing
Scenario
Before
After
10.3 References
Unity Documentation
Design Patterns
Performance Optimization
📝 Document Revision History
Version
Date
Changes
Last updated