在Java中,实现具有多个实现类的接口并为每个实现类定义一个名字的方法,可以通过使用工厂模式或服务定位器模式来完成。以下是使用工厂模式的一个示例:
定义接口和实现类
首先,定义一个接口和多个实现类:
// 接口
public interface ServiceInterface {
void serve();
}
// 实现类1
public class ServiceImpl1 implements ServiceInterface {
@Override
public void serve() {
System.out.println("Serve method of ServiceImpl1");
}
}
// 实现类2
public class ServiceImpl2 implements ServiceInterface {
@Override
public void serve() {
System.out.println("Serve method of ServiceImpl2");
}
}
创建工厂类
然后,创建一个工厂类来根据给定的名字实例化相应的实现类:
public class ServiceFactory {
public static ServiceInterface getService(String name) {
switch (name) {
case "Impl1":
return new ServiceImpl1();
case "Impl2":
return new ServiceImpl2();
default:
throw new IllegalArgumentException("Unknown service implementation: " + name);
}
}
}
使用工厂类:
public class Main {
public static void main(String[] args) {
// 获取实现类实例
ServiceInterface service1 = ServiceFactory.getService("Impl1");
service1.serve(); // 输出: Serve method of ServiceImpl1
ServiceInterface service2 = ServiceFactory.getService("Impl2");
service2.serve(); // 输出: Serve method of ServiceImpl2
}
}
方法二:
注册机制。可以定义一个hashmap,每个实现类注册自身到工厂,这里要用到spring的initializingBean:在 Java Spring 框架中,InitializingBean 是一个接口,它定义了一个在 Bean 初始化(初始化包括依赖注入)之后执行的方法。这个接口主要用于提供自定义的初始化逻辑,当 Spring 容器创建 Bean 实例并完成依赖注入之后,这个方法会被自动调用。要实现 InitializingBean,你需要实现 afterPropertiesSet() 方法,并在其中编写初始化代码。要实现 InitializingBean,你需要实现 afterPropertiesSet() 方法,并在其中编写初始化代码。实现了 InitializingBean 接口的类可以作为 Bean 被 Spring 管理
抽象类:
@Slf4j
public abstract class ClusterHandler implements InitializingBean {
@Resource
private SecurityService securityService;
@Resource
private ResourceGroupService resourceGroupService;
@Resource
private ResourceGroupRepository resourceGroupRepository;
@Resource
private ResourceAuthorizationRepository resourceAuthorizationRepository;
/**
* 创建集群/组件
*
* @param param 集群/组件请求参数
*/
public void create(ClusterRequest param) {
throw new UnsupportedOperationException();
}
/**
* 删除集群/组件
*
* @param param 集群/组件请求参数
*/
public void delete(ClusterRequest param) {
throw new UnsupportedOperationException();
}
/**
* 获取名称
*
* @param clusterId 集群ID
* @return 集群名称
*/
public String describeClusterName(String clusterId) {
throw new UnsupportedOperationException();
}
/**
* 批量获取集群的名称
*
* @param clusterIds 集群id集合
* @return 集群id与集群名称的映射关系
*/
public Map<String, String> describeClusterNameMap(List<String> clusterIds) {
throw new UnsupportedOperationException();
}
}
工厂类:
public class ClusterFactory {
// 处理类Map
private static final Map<String, ClusterHandler> CLUSTER_FACTORY = new HashMap<>(8);
// 获取处理类
public static ClusterHandler get(String type) {
return CLUSTER_FACTORY.get(type);
}
// 注册处理类
public static void register(String type, ClusterHandler clusterHandler) {
if (null == type) {
return;
}
CLUSTER_FACTORY.put(type, clusterHandler);
}
}
实现类:
//一个实现类
@Service
@Slf4j
public class CloudClusterHandler extends ClusterHandler {
@Override
public void afterPropertiesSet() {
ClusterFactory.register(ClusterTypeEnum.CLOUD.getName(), this);
}
}