代码
package dmcore.agents.coreagents;
import java.util.HashMap;
import dmcore.agents.mytypedef.AgentFactory;
import android.util.Log;
interface FRegisterAgent{
public void DeclareAgentInterface(String sAName);
}
public class CRegistry {
// D: the AgentsRegistry object
public static CRegistry AgentsRegistry = new CRegistry();
// ----------------------------------------------------------------------------
// private members
// ---------------------------------------------------------------------------
// hash holding agent name -> agent mapping
private HashMap<String, CAgent> AgentsHash = new HashMap<String, CAgent>();
// hash holding agent type -> agent mapping
private HashMap<String, AgentFactory> AgentsTypeHash = new HashMap<String, AgentFactory>();
// -----------------------------------------------------------------------------
// Constructors and Destructors
// -----------------------------------------------------------------------------
// D: Constructor
public CRegistry() {
// nothing here
Log.d("TEST","Constructor completed");
}
// Log Tag
private static final String REGISTRY_STREAM_TAG = "REGISTRY";
// D: Initializes the registry, empties everything
public void Clear() {
Log.d(REGISTRY_STREAM_TAG, "Clearing up the registry ...");
// clear the hashes
AgentsHash.clear();
AgentsTypeHash.clear();
Log.d(REGISTRY_STREAM_TAG,
"Clearing up remaining registered agents completed.");
}
// -----------------------------------------------------------------------------
//
// Registry specific functions for registered agent names
//
// -----------------------------------------------------------------------------
// D: return a pointer to an agent, given the agent's name. Returns NULL if
// the agent is not found
public CAgent getAgentGivenName(String sAgentName) {
if(!AgentsHash.containsKey(sAgentName)) {
// if the agent is not found, return NULL
return null;
} else {
// otherwise, return the pointer to the agent
return AgentsHash.get(sAgentName);
}
}
// D: return true if the agent is already registered
public boolean IsRegisteredAgent(String sAgentName) {
return AgentsHash.containsKey(sAgentName);
}
// D: register an agent into the registry.
public void RegisterAgent(String sAgentName, CAgent pAgent) {
// check that there's no agent already registered under the same name
if (IsRegisteredAgent(sAgentName)) {
Log.e(REGISTRY_STREAM_TAG,
"An agent already registered under the same name ("
+ sAgentName + ") was found.");
}
// register the agent
AgentsHash.put(sAgentName, pAgent);
// and log that
Log.d(REGISTRY_STREAM_TAG, "Agent " + sAgentName
+ " registered successfully.");
}
// D: unregister an agent
public void UnRegisterAgent(String sAgentName) {
if (AgentsHash.remove(sAgentName) == null) {
Log.e(REGISTRY_STREAM_TAG, "Could not find agent " + sAgentName
+ " to unregister.");
}
// and log that
Log.d(REGISTRY_STREAM_TAG, "Agent " + sAgentName
+ " unregistered successfully.");
}
// D: return a pointer to an agent, given the agent's name. Returns NULL if
// the agent is not found
CAgent getAgentPointer(String sAgentName) {
if(!AgentsHash.containsKey(sAgentName)){
// if the agent is not found, return NULL
return null;
} else {
// otherwise, return the pointer to the agent
return AgentsHash.get(sAgentName);
}
}
// -----------------------------------------------------------------------------
//
// Registry specific functions for registered agent types
//
// -----------------------------------------------------------------------------
// D: return true if the agent type is already registered
public boolean IsRegisteredAgentType(String sAgentTypeName) {
return AgentsTypeHash.containsKey(sAgentTypeName);
}
// D: register an agent type into the registry.
public void RegisterAgentType(String sAgentTypeName, AgentFactory cCreateAgent) {
// check that there's no agent already registered under the same name
if (IsRegisteredAgentType(sAgentTypeName)) {
Log.e(REGISTRY_STREAM_TAG,
"An agent type already registered under the same name ("
+ sAgentTypeName + ") was found.");
}
// register the agent type
AgentsTypeHash.put(sAgentTypeName, cCreateAgent);
// and log that
Log.d(REGISTRY_STREAM_TAG, "Agent type " + sAgentTypeName
+ " registered successfully.");
}
// D: unregister an agent type
public void UnRegisterAgentType(String sAgentTypeName) {
if (AgentsTypeHash.remove(sAgentTypeName) == null) {
Log.e(REGISTRY_STREAM_TAG, "Could not find agent type"
+ sAgentTypeName + " to unregister.");
}
// and log that
Log.d(REGISTRY_STREAM_TAG, "Agent type " + sAgentTypeName
+ " unregistered successfully.");
}
// D: create a new agent from a given agent type
public CAgent CreateAgent(String sAgentTypeName, String sAgentName) {
// default AgentConfiguration value
String sAgentConfiguration="";
// test if the agent type is in the registry
if(!AgentsTypeHash.containsKey(sAgentTypeName)) {
// if the agent type is not in the registry, we're in bad shape
Log.e(REGISTRY_STREAM_TAG,"Could not create agent of type " + sAgentTypeName +
". Type not found in the registry.");
return null;
} else {
// if we found the agent, call the create method.
// AgentFactory dynamically calls the create method.
CAgent pNewAgent = AgentsTypeHash.get(sAgentTypeName).AgentFactory(sAgentName, sAgentConfiguration);
if(pNewAgent != null) {
// call the create function for the agent
pNewAgent.Create();
Log.d(REGISTRY_STREAM_TAG, "Agent "+sAgentName+" created successfully.");
// and return a pointer to it
return pNewAgent;
} else {
// if creation failed, trigger a fatal error
Log.e(REGISTRY_STREAM_TAG,"Error creating agent of type " + sAgentTypeName + ".");
return null;
}
}
}
// OverLoad: create a new agent from a given agent type,name and configuration
public CAgent CreateAgent(String sAgentTypeName, String sAgentName,String sAgentConfiguration) {
// test if the agent type is in the registry
if(!AgentsTypeHash.containsKey(sAgentTypeName)) {
// if the agent type is not in the registry, we're in bad shape
Log.e(REGISTRY_STREAM_TAG,"Could not create agent of type " + sAgentTypeName +
". Type not found in the registry.");
return null;
} else {
// if we found the agent, call the create method.
// AgentFactory dynamically calls the create method.
CAgent pNewAgent = AgentsTypeHash.get(sAgentTypeName).AgentFactory(sAgentName, sAgentConfiguration);
if(pNewAgent != null) {
// call the create function for the agent
pNewAgent.Create();
Log.d(REGISTRY_STREAM_TAG, "Agent "+sAgentName+" created successfully.");
// and return a pointer to it
return pNewAgent;
} else {
// if creation failed, trigger a fatal error
Log.e(REGISTRY_STREAM_TAG,"Error creating agent of type " + sAgentTypeName + ".");
return null;
}
}
}
}
这段代码定义了一个名为 CRegistry
的类,用于管理和注册代理(agents)。以下是这段代码的主要功能和组成部分的总结:
类定义
- 包名:
dmcore.agents.coreagents
- 导入:
java.util.HashMap
,dmcore.agents.mytypedef.AgentFactory
,android.util.Log
成员变量
AgentsRegistry
:静态成员,用于存储全局唯一的CRegistry
实例。AgentsHash
:私有成员,类型为HashMap<String, CAgent>
,用于存储代理名称到代理对象的映射。AgentsTypeHash
:私有成员,类型为HashMap<String, AgentFactory>
,用于存储代理类型名称到创建代理的工厂方法的映射。
构造函数
CRegistry()
:构造函数,用于创建CRegistry
的实例。构造函数中记录了构造完成的日志信息。
方法
Clear()
:清空所有的代理和代理类型映射。getAgentGivenName(String sAgentName)
:根据代理名称获取代理对象。如果找不到相应的代理,则返回null
。IsRegisteredAgent(String sAgentName)
:判断指定名称的代理是否已经被注册。RegisterAgent(String sAgentName, CAgent pAgent)
:注册代理到AgentsHash
中。如果代理名称已存在,则记录错误日志。UnRegisterAgent(String sAgentName)
:从AgentsHash
中移除指定名称的代理。如果找不到相应的代理,则记录错误日志。getAgentPointer(String sAgentName)
:与getAgentGivenName
功能相同,用于获取指定名称的代理对象。
IsRegisteredAgentType(String sAgentTypeName)
:判断指定类型的代理是否已经被注册。RegisterAgentType(String sAgentTypeName, AgentFactory cCreateAgent)
:注册代理类型到AgentsTypeHash
中。如果代理类型已存在,则记录错误日志。UnRegisterAgentType(String sAgentTypeName)
:从AgentsTypeHash
中移除指定类型的代理。如果找不到相应的代理类型,则记录错误日志。CreateAgent(String sAgentTypeName, String sAgentName)
:根据代理类型名称创建一个新的代理对象。如果找不到相应的代理类型,则记录错误日志并返回null
。如果创建成功,则记录日志信息并返回代理对象。CreateAgent(String sAgentTypeName, String sAgentName, String sAgentConfiguration)
:与CreateAgent(String sAgentTypeName, String sAgentName)
功能类似,但额外提供代理的配置信息。
日志记录
Log.d
:记录调试级别的日志。Log.e
:记录错误级别的日志。
总结
这个类主要负责管理代理对象
和代理类型
的注册、查询、创建和删除等功能。
它提供了一种机制来动态创建和管理不同类型的代理对象
,使得应用程序可以根据需要灵活地添加、删除或修改代理。
此外,它还提供了一些方法来检查代理和代理类型是否已被注册
,以及获取已注册的代理对象
。
分析01
我怎么调用注册代理的方法进行注册呢?就是:RegisterAgent方法
因为这个类当中定义了一个静态实例AgentsRegistry
所以我可以直接调用:CRegistry.AgentsRegistry.RegisterAgent
AgentsHash
和 AgentsTypeHash
在应用运行时会一直存在,除非你调用 Clear()
方法来清空它们。
即使调用了 Clear()
方法,这两个哈希表本身依然存在,只是它们的内容被清空了。