Skip to content

Commit

Permalink
新增创建资源对象流程模式
Browse files Browse the repository at this point in the history
  • Loading branch information
googs1025 committed Nov 12, 2023
1 parent 6b03585 commit fb216f6
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion other/Go-gorm-practice/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions other/create-resource-pattern/hook/auth_hook/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
9 changes: 9 additions & 0 deletions other/create-resource-pattern/hook/interface.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 22 additions & 0 deletions other/create-resource-pattern/hook/log_hook/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions other/create-resource-pattern/hook/validator_hook/hook.go
Original file line number Diff line number Diff line change
@@ -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)
}
97 changes: 97 additions & 0 deletions other/create-resource-pattern/manager.go
Original file line number Diff line number Diff line change
@@ -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
}
74 changes: 74 additions & 0 deletions other/create-resource-pattern/manager_test.go
Original file line number Diff line number Diff line change
@@ -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", "[email protected]")
if err != nil {
fmt.Println("Error creating resource:", err)
return
}

err = rm.CreateResource("2", "Resource 2", "[email protected]")
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)
}
9 changes: 9 additions & 0 deletions other/create-resource-pattern/resource/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package resource

// Resource 是一个示例资源对象
type Resource struct {
ID string
Name string
Email string
// 添加其他属性...
}

0 comments on commit fb216f6

Please sign in to comment.