Jack il y a 3 mois
Parent
commit
3d8b008d4e
6 fichiers modifiés avec 61 ajouts et 2 suppressions
  1. 8 0
      .idea/.gitignore
  2. 9 0
      .idea/AnimalShelter.iml
  3. 8 0
      .idea/modules.xml
  4. 6 0
      .idea/vcs.xml
  5. 10 1
      doc.md
  6. 20 1
      shelter/registry.go

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# 默认忽略的文件
+/shelf/
+/workspace.xml
+# 基于编辑器的 HTTP 客户端请求
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 9 - 0
.idea/AnimalShelter.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="WEB_MODULE" version="4">
+  <component name="Go" enabled="true" />
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/AnimalShelter.iml" filepath="$PROJECT_DIR$/.idea/AnimalShelter.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>

+ 10 - 1
doc.md

@@ -42,4 +42,13 @@
 
 ---
 
-如需完整示例代码、测试、基准和 Makefile 等,请告知。以上即为按照用户需求的详细设计与关键代码示范。
+如需完整示例代码、测试、基准和 Makefile 等,请告知。以上即为按照用户需求的详细设计与关键代码示范。
+
+
+怎么实现可扩展 -> 插件 -> 插件是什么? -> 插件的核心模块 -> 注册模式 -> 基于什么进行扩展 -> 动物 -> 需要一个支持多种动物的接口
+-> Animal 接口 -> 实现注册 -> 构建注册容器 -> 容器内装载什么类型 -> 直接装载Animal, 需要提前实例化 -> 装载 Animal 的构造器 -> 实现注册方法
+-> 结合注册提供实例构建方法 -> 提供注册服务方法 -> 注册中心构建结束 -> 
+
+怎么实现收容所 -> 收容所是什么? -> 有哪些功能模块 -> 实现相关的功能模块 -> 根据动物类型实现动物接口
+
+最终实现整个功能, 参考 main

+ 20 - 1
shelter/registry.go

@@ -1,6 +1,9 @@
 package shelter
 
-import "sync"
+import (
+	"fmt"
+	"sync"
+)
 
 type AnimlConstructor func() Animal
 
@@ -8,3 +11,19 @@ var (
 	registrMu sync.RWMutex
 	registry  = make(map[string]AnimlConstructor)
 )
+
+func Register(typeName string, constructor AnimlConstructor) {
+	registrMu.Lock()
+	defer registrMu.Unlock()
+	registry[typeName] = constructor
+}
+
+func Create(typeName string) (Animal, error) {
+	registrMu.RLock()
+	defer registrMu.RUnlock()
+	constructor, ok := registry[typeName]
+	if !ok {
+		return nil, fmt.Errorf("animl %s is not registered", typeName)
+	}
+	return constructor(), nil
+}