From fb216f61776c5a2e75d8e37972572229a32a935c Mon Sep 17 00:00:00 2001 From: googs1025 Date: Sun, 12 Nov 2023 15:51:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=9B=E5=BB=BA=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E5=AF=B9=E8=B1=A1=E6=B5=81=E7=A8=8B=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- other/Go-gorm-practice/client.go | 2 +- .../hook/auth_hook/hook.go | 26 +++++ .../create-resource-pattern/hook/interface.go | 9 ++ .../hook/log_hook/hook.go | 22 +++++ .../hook/validator_hook/hook.go | 25 +++++ other/create-resource-pattern/manager.go | 97 +++++++++++++++++++ other/create-resource-pattern/manager_test.go | 74 ++++++++++++++ .../create-resource-pattern/resource/model.go | 9 ++ 9 files changed, 264 insertions(+), 2 deletions(-) create mode 100644 other/create-resource-pattern/hook/auth_hook/hook.go create mode 100644 other/create-resource-pattern/hook/interface.go create mode 100644 other/create-resource-pattern/hook/log_hook/hook.go create mode 100644 other/create-resource-pattern/hook/validator_hook/hook.go create mode 100644 other/create-resource-pattern/manager.go create mode 100644 other/create-resource-pattern/manager_test.go create mode 100644 other/create-resource-pattern/resource/model.go diff --git a/go.mod b/go.mod index 791dbf3..51e2eeb 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module golanglearning/new_project/for-gong-zhong-hao/Design-Patterns-practice +module github.com/practice/Design-Patterns-practice go 1.18 diff --git a/other/Go-gorm-practice/client.go b/other/Go-gorm-practice/client.go index 6ac206a..a702c1b 100644 --- a/other/Go-gorm-practice/client.go +++ b/other/Go-gorm-practice/client.go @@ -3,7 +3,7 @@ package main import ( "errors" "fmt" - model "golanglearning/new_project/for-gong-zhong-hao/Design-Patterns-practice/other/Go-gorm-practice/gorm_demo" + model "github.com/practice/Design-Patterns-practice/other/Go-gorm-practice/gorm_demo" "gorm.io/gorm" "log" "math/rand" diff --git a/other/create-resource-pattern/hook/auth_hook/hook.go b/other/create-resource-pattern/hook/auth_hook/hook.go new file mode 100644 index 0000000..b85ecc7 --- /dev/null +++ b/other/create-resource-pattern/hook/auth_hook/hook.go @@ -0,0 +1,26 @@ +package auth_hook + +import ( + "fmt" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/resource" +) + +// AuthHook 是身份验证的钩子实现 +type AuthHook struct { + Username string + Password string +} + +// BeforeCreate 在创建资源之前执行的钩子函数 +func (h *AuthHook) BeforeCreate(resource *resource.Resource) error { + // 模拟身份验证逻辑 + if h.Username != "admin" || h.Password != "password" { + return fmt.Errorf("authentication failed") + } + return nil +} + +// AfterCreate 在创建资源之后执行的钩子函数 +func (h *AuthHook) AfterCreate(resource *resource.Resource) { + fmt.Printf("Resource with ID %s created successfully\n", resource.ID) +} diff --git a/other/create-resource-pattern/hook/interface.go b/other/create-resource-pattern/hook/interface.go new file mode 100644 index 0000000..73b0857 --- /dev/null +++ b/other/create-resource-pattern/hook/interface.go @@ -0,0 +1,9 @@ +package hook + +import "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/resource" + +// ResourceHook 创建资源时的钩子方法 +type ResourceHook interface { + BeforeCreate(resource *resource.Resource) error + AfterCreate(resource *resource.Resource) +} diff --git a/other/create-resource-pattern/hook/log_hook/hook.go b/other/create-resource-pattern/hook/log_hook/hook.go new file mode 100644 index 0000000..9694a2d --- /dev/null +++ b/other/create-resource-pattern/hook/log_hook/hook.go @@ -0,0 +1,22 @@ +package log_hook + +import ( + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/resource" + "log" +) + +// LoggingHook 是日志记录的钩子实现 +type LoggingHook struct { + Logger *log.Logger +} + +// BeforeCreate 在创建资源之前执行的钩子函数 +func (h *LoggingHook) BeforeCreate(resource *resource.Resource) error { + h.Logger.Printf("Creating resource with ID: %s\n", resource.ID) + return nil +} + +// AfterCreate 在创建资源之后执行的钩子函数 +func (h *LoggingHook) AfterCreate(resource *resource.Resource) { + h.Logger.Printf("Resource with ID %s created successfully\n", resource.ID) +} diff --git a/other/create-resource-pattern/hook/validator_hook/hook.go b/other/create-resource-pattern/hook/validator_hook/hook.go new file mode 100644 index 0000000..cb26a3e --- /dev/null +++ b/other/create-resource-pattern/hook/validator_hook/hook.go @@ -0,0 +1,25 @@ +package validator_hook + +import ( + "fmt" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/resource" + "regexp" +) + +// EmailValidatorHook 是电子邮件验证的钩子实现 +type EmailValidatorHook struct { + EmailRegex *regexp.Regexp +} + +// BeforeCreate 在验证用户之前执行的钩子函数 +func (h *EmailValidatorHook) BeforeCreate(resource *resource.Resource) error { + if !h.EmailRegex.MatchString(resource.Email) { + return fmt.Errorf("invalid email address") + } + return nil +} + +// AfterCreate 在验证用户之后执行的钩子函数 +func (h *EmailValidatorHook) AfterCreate(resource *resource.Resource) { + fmt.Printf("User with ID %s validated successfully\n", resource.ID) +} diff --git a/other/create-resource-pattern/manager.go b/other/create-resource-pattern/manager.go new file mode 100644 index 0000000..1563ea8 --- /dev/null +++ b/other/create-resource-pattern/manager.go @@ -0,0 +1,97 @@ +package create_resource_pattern + +import ( + "fmt" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/hook" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/resource" + "sync" +) + +// ResourceManager 是资源对象的管理器 +type ResourceManager struct { + mu sync.Mutex + resources map[string]*resource.Resource + beforeFn []func(resource *resource.Resource) error + afterFn []func(resource *resource.Resource) +} + +// NewResourceManager 创建一个新的资源管理器 +func NewResourceManager() *ResourceManager { + return &ResourceManager{ + resources: make(map[string]*resource.Resource), + } +} + +// RegisterHook 注册资源对象的钩子 +func (rm *ResourceManager) RegisterHook(hook hook.ResourceHook) { + rm.beforeFn = append(rm.beforeFn, hook.BeforeCreate) + rm.afterFn = append(rm.afterFn, hook.AfterCreate) +} + +// CreateResource 创建一个新的资源对象 +func (rm *ResourceManager) CreateResource(id, name, email string) error { + rm.mu.Lock() + defer rm.mu.Unlock() + + if _, exists := rm.resources[id]; exists { + return fmt.Errorf("resource with ID %s already exists", id) + } + + rr := &resource.Resource{ + ID: id, + Name: name, + Email: email, + // 设置其他属性... + } + + // 执行创建前的钩子函数 + for _, beforeFn := range rm.beforeFn { + err := beforeFn(rr) + if err != nil { + return err + } + } + + rm.resources[id] = rr + + // 执行创建后的钩子函数 + for _, afterFn := range rm.afterFn { + afterFn(rr) + } + + return nil +} + +// GetResourceByID 根据资源 ID 获取资源对象 +func (rm *ResourceManager) GetResourceByID(id string) (*resource.Resource, error) { + rm.mu.Lock() + defer rm.mu.Unlock() + + r, exists := rm.resources[id] + if !exists { + return nil, fmt.Errorf("resource with ID %s does not exist", id) + } + + return r, nil +} + +func (rm *ResourceManager) DeleteResourceByID(id string) error { + rm.mu.Lock() + defer rm.mu.Unlock() + + delete(rm.resources, id) + return nil +} + +// ListResources 返回所有资源对象的列表 +func (rm *ResourceManager) ListResources() []*resource.Resource { + rm.mu.Lock() + defer rm.mu.Unlock() + + resources := make([]*resource.Resource, 0, len(rm.resources)) + for _, r := range rm.resources { + resources = append(resources, r) + } + + return resources +} diff --git a/other/create-resource-pattern/manager_test.go b/other/create-resource-pattern/manager_test.go new file mode 100644 index 0000000..7164e0a --- /dev/null +++ b/other/create-resource-pattern/manager_test.go @@ -0,0 +1,74 @@ +package create_resource_pattern + +import ( + "fmt" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/hook/auth_hook" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/hook/log_hook" + "github.com/practice/Design-Patterns-practice/other/create-resource-pattern/hook/validator_hook" + "log" + "regexp" + "testing" +) + +func TestResourceManager(t *testing.T) { + rm := NewResourceManager() + + // 创建一个身份验证钩子对象 + authHook := &auth_hook.AuthHook{ + Username: "admin", + Password: "password", + } + + // 注册身份验证钩子 + rm.RegisterHook(authHook) + + // 创建一个日志记录钩子对象 + logger := log.New(log.Writer(), "", log.LstdFlags) + loggingHook := &log_hook.LoggingHook{ + Logger: logger, + } + + // 注册日志记录钩子 + rm.RegisterHook(loggingHook) + + // 创建一个电子邮件验证钩子对象 + emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) + + emailValidator := &validator_hook.EmailValidatorHook{ + EmailRegex: emailRegex, + } + + rm.RegisterHook(emailValidator) + + err := rm.CreateResource("1", "Resource 1", "john_doe11@example.com") + if err != nil { + fmt.Println("Error creating resource:", err) + return + } + + err = rm.CreateResource("2", "Resource 2", "john_doe@example.com") + if err != nil { + fmt.Println("Error创建资源:", err) + return + } + + resources := rm.ListResources() + fmt.Println("Resources:") + for _, resource := range resources { + fmt.Printf("ID: %s, Name: %s\n", resource.ID, resource.Name) + } + + resource, err := rm.GetResourceByID("1") + if err != nil { + fmt.Println("Error getting resource:", err) + return + } + + err = rm.DeleteResourceByID("1") + if err != nil { + fmt.Println("Error deleting resource:", err) + return + } + + fmt.Println("Resource 1 Name:", resource.Name) +} diff --git a/other/create-resource-pattern/resource/model.go b/other/create-resource-pattern/resource/model.go new file mode 100644 index 0000000..6c3c3e6 --- /dev/null +++ b/other/create-resource-pattern/resource/model.go @@ -0,0 +1,9 @@ +package resource + +// Resource 是一个示例资源对象 +type Resource struct { + ID string + Name string + Email string + // 添加其他属性... +}