gpt4 book ai didi

Java - 服务提供者的实现

转载 作者:行者123 更新时间:2023-12-01 15:30:27 24 4
gpt4 key购买 nike

我在 C# 中有以下“模式”(不是真正公认的 DP,但我倾向于在我的解决方案中经常使用它),其目标是基于接口(interface)(某种类型)提供对象实现的中心点工厂)。

它作为单例进行访问,用户请求给定接口(interface)的实现并返回适当的实现。

我的目标是将其迁移到 Java。我有两个原型(prototype)解决方案,但我对结果不太满意,因为在其中一个(更强大和复杂)上,由于 Java 的泛型限制,我不得不在实例化机制之上放置大量抽象尽管更简单,但功能也弱得多(它甚至无法利用泛型 - 这在我的使用情况下可能很方便)。

我想通过以下方式在 Java 中使用它来注册新的实现:

ServiceFactory.getInstance().addService(IMyInterface.class, new MyImplementation());

(其中 MyImplementations 实现 IMyInterface)。

遵循原始 C# 版本和两个 Java 版本:

C# 代码

public class ObjectProvider<ObjectType, BaseObjectType> : IObjectProvider<BaseObjectType>
where ObjectType : BaseObjectType, new()
{

public BaseObjectType ObjectInstance
{
get { return (BaseObjectType)new ObjectType(); }
}
}

public class ServiceManager
{
private static Dictionary<Type, object> _providersList = null;
private static object _listLocker = new object();

private ServiceManager() { }

private static void InicializeProvidersList()
{
_providersList = new Dictionary<Type, object>();

_providersList.Add(typeof(IMyType), new ObjectProvider<MyImplementation, IMyType>());
...
}

private static Dictionary<Type, object> ProvidersList
{
get
{
lock (_listLocker)
{
if (_providersList == null)
InicializeProvidersList();
return _providersList;
}
}
}

public static BusinessType GetBusinessClass<BusinessType>()
{
Dictionary<Type, object> list = ProvidersList;
Type pretendedType = typeof(BusinessType);
if (!list.ContainsKey(pretendedType))
return default(BusinessType);
IObjectProvider<BusinessType> provider = list[pretendedType] as IObjectProvider<BusinessType>;
return provider.ObjectInstance;
}
}

Java(更强大的解决方案)代码

public interface IInstantiator 
{
<BaseObjectType> BaseObjectType getInstance(Object... args);
void setCacheable(boolean value);
}

public abstract class BaseInstantiator <BaseObjectType, ObjectType extends BaseObjectType> implements IInstantiator
{
protected Class<ObjectType> objectType;
protected boolean isCacheable = true;
protected BaseObjectType cache;

public BaseInstantiator(Class<ObjectType> objectType)
{
this.objectType = objectType;
}

public void setCacheable(boolean value)
{
this.isCacheable = value;
}

@SuppressWarnings("unchecked")
public final BaseObjectType getInstance(Object... args)
{
if(isCacheable && cache != null)
{
return cache;
}
else
{
BaseObjectType objectType = createInstance(args);

if(isCacheable)
{
cache = objectType;
}

return objectType;
}
}

protected abstract BaseObjectType createInstance(Object... args);
}

public class Instantiator <BaseObjectType, ObjectType extends BaseObjectType> extends BaseInstantiator <BaseObjectType, ObjectType>
{

public Instantiator(Class<ObjectType> ot)
{
super(ot);
}

@Override
protected BaseObjectType createInstance(Object... args)
{
try
{
return objectType.newInstance();
}
catch (InstantiationException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}

return null;
}
}


public class ServiceFactory
{
private HashMap<Class, IInstantiator> services;
private static ServiceFactory instance;

public <BaseObjectType> void addService(Class<BaseObjectType> baseObjectType, IInstantiator instantiator)
{
this.getServices().put(baseObjectType, instantiator);
}

@SuppressWarnings("unchecked")
public <BaseObjectType> BaseObjectType getService(Class<BaseObjectType> baseObjectType, Object... args)
{

if(! getServices().containsKey(baseObjectType))
{
throw new NoSuchElementException("Unknown service interface!");
}
else
{
try
{
return (BaseObjectType) getServices().get(baseObjectType).getInstance(args);
}
catch (Exception e)
{
return null;
}
}
}

private ServiceFactory () { }

public static synchronized ServiceFactory getInstance()
{

if(ServiceFactory.instance == null)
{
ServiceFactory.instance = new ServiceFactory();
populate();
}

return ServiceFactory.instance;
}

private static void populate()
{
//...
}

private HashMap<Class, IInstantiator> getServices()
{

if(this.services == null)
{
this.services = new HashMap<Class, IInstantiator>();
}

return this.services;
}
}

Java(更简单、功能更弱的解决方案)

@SuppressWarnings("rawtypes")
public class ManagerFactory
{
private Map<Class, Object> managers;

private ManagerFactory()
{
populateFactory();
}

private static class SingletonHolder
{
public static final ManagerFactory INSTANCE = new ManagerFactory();
}

public static ManagerFactory getInstance()
{
return SingletonHolder.INSTANCE;
}

private void populateFactory()
{
this.managers = new HashMap<Class, Object>();

this.managers.put(ITaskManager.class, new TaskManager());
}

public Object getManager(Class interfaceClass)
{
if(this.managers.containsKey(interfaceClass))
return this.managers.get(interfaceClass);
else
return null;
}
}

任何人都可以提供一些帮助吗?

提前致谢!

最佳答案

这通常称为 Service Locator pattern .

我会推荐一个预构建的解决方案,例如 robo-guice .

关于Java - 服务提供者的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9582670/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com