Go Design Pattern: Chain of Responsibility

    📚 Go Design Patterns 🎯Behavioral Pattern ← Command Pattern 📋 All Patterns Observer Pattern → What is Chain of Responsibility? The Chain of Responsibility pattern is a behavioral design pattern that allows you to pass requests along a chain of handlers. Each handler decides whether to process the request or pass it to the next handler in the chain. This pattern decouples the sender of a request from its receivers by giving multiple objects a chance to handle the request. ...

    January 15, 2025 · 13 min · Rafiul Alam

    Go Design Pattern: Iterator

    📚 Go Design Patterns 🎯Behavioral Pattern ← Observer Pattern 📋 All Patterns State Pattern → What is Iterator Pattern? The Iterator pattern provides a way to access elements of a collection sequentially without exposing the underlying representation. It’s like having a remote control for your TV - you don’t need to know how the channels are stored internally, you just press “next” to move through them. ...

    November 13, 2024 · 11 min · Rafiul Alam

    Saving Game State: Implementing the Memento Pattern with encoding/gob

    The Save/Load Problem Every game needs to save player progress. But how do you capture the entire game state without exposing internal implementation details? How do you support undo/redo, time travel debugging, or replay systems? The Memento pattern solves this by capturing and externalizing an object’s internal state without violating encapsulation. Combined with Go’s encoding/gob package, you get powerful, type-safe serialization for game saves, undo systems, and more. The Naive Approach Here’s what not to do: ...

    October 12, 2024 · 9 min · Rafiul Alam

    Go Design Pattern: State

    What is State Pattern? The State pattern is a behavioral design pattern that allows an object to alter its behavior when its internal state changes. It appears as if the object changed its class. Think of it like a vending machine - it behaves differently when it’s waiting for coins, has coins inserted, is dispensing a product, or is out of stock. Each state has its own set of valid operations and transitions. ...

    August 5, 2024 · 11 min · Rafiul Alam

    Go Design Pattern: Template Method

    What is Template Method Pattern? The Template Method pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class and lets subclasses override specific steps without changing the algorithm’s structure. Think of it like a recipe - the overall cooking process is the same (prepare ingredients, cook, serve), but the specific steps can vary depending on what you’re making. In Go, since we don’t have traditional inheritance, we implement this pattern using composition and interfaces, which actually makes it more flexible and idiomatic. ...

    July 10, 2024 · 11 min · Rafiul Alam

    Data-Oriented Design: Implementing ECS (Entity Component System) with Go Generics

    From Object-Oriented to Data-Oriented Traditional object-oriented programming (OOP) encourages you to model game entities as objects with inheritance hierarchies. While intuitive, this approach leads to poor cache locality, rigid hierarchies, and performance bottlenecks. Data-oriented design, particularly the Entity Component System (ECS) pattern, flips this on its head. With Go 1.18+ generics, we can now build type-safe ECS architectures that deliver both performance and flexibility. Let me show you how. The OOP Problem Here’s the typical OOP approach to game entities: ...

    July 8, 2024 · 9 min · Rafiul Alam

    Swapping Logic at Runtime: The Strategy Pattern using First-Class Functions

    Beyond Rigid If-Else Logic Traditional approaches to varying behavior use conditional statements scattered throughout code. Need different sorting algorithms? Different pricing strategies? Different attack behaviors in a game? You end up with messy switch statements and tightly coupled code. The Strategy pattern lets you encapsulate algorithms and swap them at runtime. In Go, first-class functions make this pattern beautifully simple-no heavyweight class hierarchies required. The Traditional Approach: Conditionals Everywhere Here’s what happens without the Strategy pattern: ...

    June 17, 2024 · 8 min · Rafiul Alam

    Go Design Pattern: Command

    What is Command Pattern? The Command pattern is a behavioral design pattern that turns a request into a stand-alone object containing all information about the request. This transformation allows you to parameterize methods with different requests, delay or queue a request’s execution, and support undoable operations. Think of it like a remote control - each button is a command that encapsulates the action to be performed on a device. I’ll demonstrate how this pattern can help you build flexible, undoable, and queueable operations in Go applications. ...

    June 15, 2024 · 9 min · Rafiul Alam

    Building Modular Game AI: Implementing Behavior Trees with Go Interfaces

    Why Behavior Trees for Game AI? If you’ve ever built game AI using finite state machines, you’ve likely hit their limitations: rigid transitions, difficulty composing behaviors, and maintenance nightmares. Behavior trees offer a more flexible, modular approach that’s become the industry standard for game AI. In this post, I’ll show you how to implement behavior trees in Go using interfaces, creating reusable AI components that can power everything from enemy NPCs to complex AI companions. ...

    May 22, 2024 · 10 min · Rafiul Alam

    Go Design Pattern: Proxy

    What is Proxy Pattern? The Proxy pattern is a structural design pattern that provides a placeholder or surrogate for another object to control access to it. Think of it like a security guard at a building entrance - they control who can enter, when they can enter, and might even log who visited. The proxy acts as an intermediary that can add functionality like caching, logging, access control, or lazy loading without changing the original object. ...

    May 20, 2024 · 8 min · Rafiul Alam