strategy

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package strategy implements the Strategy design pattern. The User struct acts as a Context that can change its behavior dynamically at runtime by switching between different UserDataEncoder strategies. Each concrete strategy (MD5HashingStrategy, Base64EncodingStrategy or ReverseEncodingStrategy) defines a unique algorithm for encoding user data such as login, name, and surname.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run()

Types

type Base64EncodingStrategy

type Base64EncodingStrategy struct{}

Base64EncodingStrategy implements the Base64 encoding strategy for the User's data.

func (*Base64EncodingStrategy) EncodeLogin

func (b *Base64EncodingStrategy) EncodeLogin(user *User) string

EncodeLogin allows to encode the User's login.

func (*Base64EncodingStrategy) EncodeName

func (b *Base64EncodingStrategy) EncodeName(user *User) string

EncodeName allows to encode the User's name.

func (*Base64EncodingStrategy) EncodeSurname

func (b *Base64EncodingStrategy) EncodeSurname(user *User) string

EncodeSurname allows to encode the User's surname.

type MD5HashingStrategy

type MD5HashingStrategy struct{}

MD5HashingStrategy implements the MD5 hashing strategy for the User's data.

func (*MD5HashingStrategy) EncodeLogin

func (m *MD5HashingStrategy) EncodeLogin(user *User) string

EncodeLogin allows to encode the User's login.

func (*MD5HashingStrategy) EncodeName

func (m *MD5HashingStrategy) EncodeName(user *User) string

EncodeName allows to encode the User's name.

func (*MD5HashingStrategy) EncodeSurname

func (m *MD5HashingStrategy) EncodeSurname(user *User) string

EncodeSurname allows to encode the User's surname.

type ReverseEncodingStrategy

type ReverseEncodingStrategy struct{}

ReverseEncodingStrategy implements the custom encoder that encodes the User's data by reversing the elements of it.

func (*ReverseEncodingStrategy) EncodeLogin

func (r *ReverseEncodingStrategy) EncodeLogin(user *User) string

EncodeLogin allows to encode the User's login.

func (*ReverseEncodingStrategy) EncodeName

func (r *ReverseEncodingStrategy) EncodeName(user *User) string

EncodeName allows to encode the User's name.

func (*ReverseEncodingStrategy) EncodeSurname

func (r *ReverseEncodingStrategy) EncodeSurname(user *User) string

EncodeSurname allows to encode the User's surname.

type Strategy

type Strategy byte
const (
	MD5 Strategy = iota
	Base64
	Reversed
)

Strategies enum:

type StrategyChanger

type StrategyChanger interface {
	ChangeStrategy(strategy Strategy)
}

StrategyChanger defines the changing strategy interface for User's data structures.

type User

type User struct {
	Login   string
	Name    string
	Surname string
	// contains filtered or unexported fields
}

User represents a the user entity whose data can be encoded using different strategies.

func NewUser

func NewUser(login, name, surname string, separator rune) *User

NewUser constructs a user with assigned parameters and default encoder

func (*User) ChangeStrategy

func (u *User) ChangeStrategy(strategy Strategy)

ChangeStrategy switches the encoding algorithm used for user data.

func (*User) String

func (u *User) String() string

String returns stringified version of encoded user data, with the parts separated by predefined delimiter.

type UserDataEncoder

type UserDataEncoder interface {
	EncodeName(user *User) string
	EncodeSurname(user *User) string
	EncodeLogin(user *User) string
}

UserDataEncoder defines the general interface for all User's data encoding strategies.

Jump to

Keyboard shortcuts

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