简体   繁体   English

如何限制java接口在多个类中实现

[英]how to restrict java interface to be implemented in more than one class

I need to develop interface which can be implemented only once. 我需要开发只能实现一次的接口。 If other class try to implement same interface in same project then it should not be allowed or give an error. 如果其他类尝试在同一个项目中实现相同的接口,则不应该允许它或给出错误。

interface A {
   void someMethod();
}

class B implements A {
    void someMethod() {
        // implementation here
    }
}

Now I want to restrict other classes to implement interface A 现在我想限制其他类来实现接口A.

class c implements A { //this  should not allowed in this project
}

Is it possible to develop this kind of interface? 是否有可能开发这种界面? Can anyone suggest, how can I go through to achieve this? 任何人都可以建议,我怎样才能实现这一目标?

简单的回答,如果你的界面是公共/包受保护的,那就不可能了。

This defeats the purpose of an interface. 这违背了界面的目的。 If you're only going to have one implementation, it may as well be concrete. 如果你只有一个实现,它也可能是具体的。

Interfaces are meant to be implemented by multiple classes. 接口意味着由多个类实现。 This allows you to switch out implementations without having to worry about their implementation details. 这允许您切换实现,而不必担心其实现细节。 For example, the most common use of interfaces is with the collections framework, particularly List , Set , and Map . 例如,接口最常见的用途是集合框架,特别是ListSetMap

// Hides the implementation details of ArrayList within a List variable
List<String> strs = new ArrayList<String>();
// Hides the implementation details of LinkedList within the same List variable
strs = new LinkedList<String>();
// All code using strs is agnostic to what kind of list it is (mostly)
strs.add("Hello, Dolly");
System.out.println(strs.get(0));

Interfaces primarily embody two OOP concepts: encapsulation and polymorphism . 接口主要包含两个OOP概念: 封装多态 If you don't plan on using your interface to accomplish one of these two things, don't use an interface. 如果您不打算使用界面来完成这两件事之一,请不要使用界面。 Just use a concrete (non-abstract) class. 只需使用具体(非抽象)类。 Using an interface at this point is overkill. 此时使用界面是过度的。

Only exception to this rule I can think of is when you want to use Java's Proxy class. 我能想到的唯一例外是当你想要使用Java的Proxy类时。 Only then is a 1:1 interface:class ratio acceptable since you have to have an interface to wrap the implementation in the Proxy instance. 只有这样才能接受1:1的接口:类比率,因为你必须有一个接口来封装Proxy实例中的实现。

It sounds like your design is wrong, and that your interface should actually just be part of class B . 听起来你的设计是错误的,你的界面实际上应该只是class B一部分。

The point of an interface is that it allows different implementations of the same set of methods, which you are trying to avoid here. 接口的意义在于它允许同一组方法的不同实现,您在此尝试避免这些方法。

Put your interface A with class B in same package. 将您的interface Aclass B放在同一个包中。

All the classes which should not implement A , should be outside this package. 所有不应该实现A的类都应该在这个包之外。

The only global way to know about implementations of A is if they register themselves to you, which is pointless, you can forget about that. 了解A实现的唯一全局方式是,如果他们向您注册,这是毫无意义的,您可以忘记这一点。


I'm not sure if you require that all instances of A that you work with have a common class besides Object.class , or that their class is the same, or that their class is B.class . 我不确定你是否要求你使用的所有A实例都有一个除了Object.class之外的公共类,或者它们的类是相同的,或者它们的类是B.class

Regardless of what you want to enforce, you need to test for undesirable situations on instances of A that you get passed, you have no business with instances of A that you don't work with anyway. 无论你想要强制执行什么,你都需要测试你传递的A实例的不良情况,你没有与A实例无关的事情。

Alternatively, for every instance of A you get passed, you could create a new B and insert the data that you need from the passed A . 或者,对于A每个实例,您都可以创建一个新的B并从传递的A插入所需的数据。


But first, you should think about why you want this, chances are it's not really a problem when there are different implementations of A . 但首先,你应该考虑为什么你想要这个,当有不同的A实现时,它可能不是真正的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM