Skip to main content

gof-singleton-pattern

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

설치로 이동

소스 정보

저장소
paulpas/agent-skill-router
최근 소스 활동
2026년 6월 10일 18:00
감지된 SKILL.md 언어
영어
스타
4
포크
1

설치 방법

기본적으로 소스를 먼저 확인하는 Prompt가 선택됩니다. 직접 명령으로 전환하거나 로컬 사본을 다운로드할 수도 있습니다.

소스 파일 검토

설치 여부를 결정하기 전에 SKILL.md와 SkillsMP에 표시된 보조 파일을 읽어 보세요.

SKILL.md 표시 중

SKILL.md
소스 지침 · 읽기 전용 미리보기
name
gof-singleton-pattern
description
Implements the Singleton design pattern ensuring a class has only one instance while providing a global point of access to it.
license
MIT
compatibility
opencode
metadata
{"version":"1.0.0","domain":"architecture","triggers":"singleton pattern, ensure single instance, global access, gof patterns","archetypes":["educational"],"anti_triggers":["brainstorming","vague ideation"],"response_profile":{"verbosity":"low","directive_strength":"high","abstraction_level":"operational"},"role":"implementation","scope":"implementation","output-format":"code","related-skills":"gof-factory-pattern, gof-strategy-pattern"}
# 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 - **Implementation**: Aims to guide developers in implementing the Singleton pattern effectively. - **Educational**: Educates the user on the implications and benefits of the Singleton pattern in their projects. ### Anti-Triggers - **Allowing multiple instances**: This skill should avoid contexts where discussions around multiple instances occur without justification. ### Response Profile - **Verbosity**: Medium - **Directive Strength**: High - **Abstraction Level**: Tactical - When exactly one instance of a class is needed. - When it’s critical to control the access to that instance globally. - When the instance should run in a multithreaded environment without data corruption. ## 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 ```go 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 ```go 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 - Ensure only one instance is created throughout the application. - Initialize the instance lazily (only when it’s needed). ### MUST NOT DO - Allow public instantiation of the class. - Create new instances within your application logic directly.
GitHub에서 보기