factory

package
v1.5.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2026 License: Apache-2.0 Imports: 4 Imported by: 8

README

Factory Package Improvements

Overview

The factory package is responsible for creating and managing plugins in the Lynx framework. After refactoring, we improved naming, interface design, and separation of concerns to make it clearer and easier to use.

File Structure

1. interfaces.go - Interface Definitions
  • Purpose: Defines the core interfaces for plugin management
  • Key components:
    • Registry interface: plugin registration management
    • Creator interface: plugin creation
    • Factory interface: full plugin management
2. registry.go - Plugin Registry
  • Purpose: Implements plugin registration and configuration mapping
  • Key components:
    • PluginRegistry struct: registry implementation
    • GlobalPluginRegistry() function: get the global registry instance
    • Register, unregister, and query capabilities
3. typed_factory.go - Type-Safe Factory
  • Purpose: Provides type-safe plugin creation and management
  • Key components:
    • TypedFactory struct: type-safe plugin factory
    • RegisterTypedPlugin() function: register type-safe plugins
    • GetTypedPlugin() function: get type-safe plugin instances
    • GlobalTypedFactory() function: get the global type-safe factory

Key Improvements

1. Naming
  • Before: lynx_factory.go, plugin_factory.go
  • After: registry.go, interfaces.go, typed_factory.go
  • Benefit: More intuitive names with clearer responsibilities
2. Interface Design
  • Problem: PluginFactory interface carried too many responsibilities
  • Solution:
    • Registry interface: focused on registration
    • Creator interface: focused on creation
    • Factory interface: composes the two
3. Type Naming
  • Before: LynxPluginFactory, TypedPluginFactory
  • After: PluginRegistry, TypedFactory
  • Benefit: Avoid confusion with the Lynx framework itself; simpler naming
4. Separation of Concerns
  • Registry: registration, deregistration, and queries
  • Factory: creation and type safety
  • Interfaces: clear contracts
5. Concurrency Safety
  • TypedFactory uses RW locks to protect concurrent access
  • Provides thread-safe plugin management

Usage Examples

Basic Registry Usage
// Get the global registry
registry := factory.GlobalPluginRegistry()

// Register a plugin
registry.RegisterPlugin("http_server", "http", func() plugins.Plugin {
    return &httpServerPlugin{}
})

// Create a plugin
plugin, err := registry.CreatePlugin("http_server")
Type-Safe Factory Usage
// Get the global type-safe factory
typedFactory := factory.GlobalTypedFactory()

// Register a type-safe plugin
factory.RegisterTypedPlugin(typedFactory, "redis", "cache", func() *redis.Plugin {
    return redis.New()
})

// Get a type-safe plugin instance
redisPlugin, err := factory.GetTypedPlugin[*redis.Plugin](typedFactory, "redis")

Backward Compatibility

To maintain backward compatibility, we kept the following:

  • TypedFactory implements the Factory interface
  • Compatible methods for legacy APIs

Interface Hierarchy

Factory (full capabilities)
├── Registry (registration)
│   ├── RegisterPlugin()
│   ├── UnregisterPlugin()
│   ├── GetPluginRegistry()
│   └── HasPlugin()
└── Creator (creation)
    └── CreatePlugin()

Design Principles

  1. Single Responsibility: each interface and struct has a clear responsibility
  2. Interface Segregation: split large interfaces into smaller specialized ones
  3. Type Safety: generics to ensure type safety
  4. Concurrency Safety: proper locking to protect shared state
  5. Backward Compatibility: keep compatibility with existing code

Documentation

Overview

Package factory provides functionality for creating and managing plugins in the Lynx framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateTypedPlugin

func CreateTypedPlugin[T plugins.Plugin](factory *TypedFactory, name string) (T, error)

CreateTypedPlugin creates a type-safe plugin instance

func GetTypedPlugin

func GetTypedPlugin[T plugins.Plugin](factory *TypedFactory, name string) (T, error)

GetTypedPlugin gets a type-safe plugin instance

func RegisterTypedPlugin

func RegisterTypedPlugin[T plugins.Plugin](
	factory *TypedFactory,
	name string,
	configPrefix string,
	creator func() T,
)

RegisterTypedPlugin registers a type-safe plugin

Types

type Creator

type Creator interface {
	// CreatePlugin instantiates a new plugin instance by its name.
	// Returns an error if the plugin cannot be created.
	CreatePlugin(name string) (plugins.Plugin, error)
}

Creator defines the interface for creating plugin instances.

type Factory

type Factory interface {
	Creator
	Registry
}

Factory defines the complete interface for plugin management, combining both creation and registry capabilities.

type Registry

type Registry interface {
	// RegisterPlugin adds a new plugin to the registry with its configuration prefix
	// and creation function.
	RegisterPlugin(name string, configPrefix string, creator func() plugins.Plugin)

	// UnregisterPlugin removes a plugin from the registry.
	UnregisterPlugin(name string)

	// GetPluginRegistry returns the mapping of configuration prefixes to plugin names.
	GetPluginRegistry() map[string][]string

	// HasPlugin checks if a plugin is registered with the given name.
	HasPlugin(name string) bool
}

Registry defines the interface for managing plugin registrations.

type TypedFactory

type TypedFactory struct {
	// contains filtered or unexported fields
}

TypedFactory type-safe plugin factory

func GlobalTypedFactory

func GlobalTypedFactory() *TypedFactory

GlobalTypedFactory returns the global type-safe plugin factory.

func NewTypedFactory

func NewTypedFactory() *TypedFactory

NewTypedFactory creates a type-safe plugin factory

func (*TypedFactory) CreatePlugin

func (f *TypedFactory) CreatePlugin(name string) (plugins.Plugin, error)

CreatePlugin creates plugin instance (compatible with old interface)

func (*TypedFactory) GetConfigMapping

func (f *TypedFactory) GetConfigMapping() map[string][]string

GetConfigMapping gets configuration mapping

func (*TypedFactory) GetPluginRegistry

func (f *TypedFactory) GetPluginRegistry() map[string][]string

GetPluginRegistry returns the plugin registry (backward-compatible API).

func (*TypedFactory) HasPlugin

func (f *TypedFactory) HasPlugin(name string) bool

HasPlugin checks if plugin exists

func (*TypedFactory) RegisterPlugin

func (f *TypedFactory) RegisterPlugin(name string, configPrefix string, creator func() plugins.Plugin)

RegisterPlugin registers a plugin (backward-compatible signature for TypedFactory). Recommended usage in plugins: factory.GlobalTypedFactory().RegisterPlugin(...)

func (*TypedFactory) UnregisterPlugin

func (f *TypedFactory) UnregisterPlugin(name string)

UnregisterPlugin unregisters a plugin.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL