注册模式(Registry)也叫做注册树模式,注册器模式。注册模式为应用中经常使用的对象创建一个中央存储器来存放这些对象 —— 通常通过一个只包含静态方法的抽象类来实现(或者通过单例模式)。
type GolangLTD struct {
Data map[string]*interface{}
}
func (this *GolangLTD)Set(key string,data *interface{}) {
this.Data[key] = data
}
func (this *GolangLTD)Get(key string) interface{} {
return this.Data[key]
}
类似:Proxy服务器的注册协议:
const (
INIYPROXY = iota // ==0
C2Proxy_SendDataProto // C2Proxy_SendDataProto == 1 客户端发送协议
Proxy2C_SendDataProto // Proxy2C_SendDataProto == 2
G2Proxy_ConnDataProto // G2Proxy_ConnDataProto == 3 服务器链接协议
Proxy2G_ConnDataProto // Proxy2G_ConnDataProto == 4
G2Proxy_SendDataProto // G2Proxy_SendDataProto == 5 服务器发送代理
Proxy2G_SendDataProto // Proxy2G_SendDataProto == 6
C2Proxy_ConnDataProto // C2Proxy_ConnDataProto == 7 客户端连接协议
Proxy2C_ConnDataProto // Proxy2C_ConnDataProto == 8
)
case float64(Proto_Proxy.C2Proxy_ConnDataProto):
{ // 客户端连接协议
ConnectionData.User_Login(ProtocolData)
}
case float64(Proto_Proxy.G2Proxy_ConnDataProto):
{ // 服务器连接协议
ConnectionData.Server_Login(ProtocolData)
}
//注册树模式
class Register
{
//这个变量应该能是全局的
protected static $trees;
//注册到注册树上方法
public static function set($alias, $obj)
{
self::$trees[$alias] = $obj;
}
//获取对象
public static function get($alias)
{
return self::$trees[$alias];
}
//释放注册对象
public function _unset($alias)
{
unset(self::$trees[$alias]);
}
}
Register::set('stdClass',new stdClass());
$getStd = Register::get('stdClass');
print_r($getStd);
参考资料:
Go语言设计模式
https://laravelacademy.org/post/2850.html
设计模式
https://www.cnblogs.com/webph/p/6745203.html