Gof singleton pattern

From paulpas/agent-skill-router · View interactive version

Gof singleton pattern is an AI agent skill from the GitHub repository paulpas/agent-skill-router. Implements the Singleton design pattern ensuring a class has only one instance while providing a global point of access to it.

How to use this skill

Skill file

Singleton Pattern

archetypes: implementation, educational

anti_triggers: allowing multiple instances

response_profile:

verbosity: medium

directive_strength: high

abstraction_level: tactical

Implements the Singleton design pattern ensuring a class has only one instance while providing a global point of access to it.

When to Use

The When to Use section should detail under what circumstances this skill is applicable by incorporating the following:

Archetypes

Anti-Triggers

Response Profile

Core Workflow

  1. Define the Class: Create a private constructor to prevent direct initialization.
  2. Static Method: Implement a static method that provides access to the instance, creating it if necessary.
  3. Provide Thread Safety: Ensure the method is thread-safe to prevent multiple instances in concurrent environments.

Implementation Patterns

Thread-Safe Singleton Pattern

package main

import (
    "sync"
)

type singleton struct {
    // example resource
    resource string
}

var instance *singleton
var once sync.Once

// GetInstance returns the single instance of the singleton class
func GetInstance() *singleton {
    once.Do(func() {
        instance = &singleton{resource: "Initialized Resource"}
    })
    return instance
}

Example Usage

package main

import (
    "fmt"
)

func main() {
    firstInstance := GetInstance()
    fmt.Println(firstInstance.resource)  // Output: Initialized Resource

    secondInstance := GetInstance()
    fmt.Println(firstInstance == secondInstance)  // Output: true
}

Constraints

MUST DO

MUST NOT DO

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.