用私有构造函数扩展类的技巧 [英] Technique for extending a class with private constructors

查看:118
本文介绍了用私有构造函数扩展类的技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种标准技术可以用私有构造函数扩展一个类,就像大多数Singleton类一样?特别是,我试图扩展 java.lang.management.ThreadInfo 类,因为我将它们中的很多添加到 HashSet code>来控制唯一性。然而,我确定两个线程是否相等的方式是不同的,并且与 equals()方法的默认实现不同。



在这种情况下,扩展类显然不是一个选项。



将类似于接受 ThreadInfo ,然后用值手动填充所有相关字段,然后覆盖 equals() hashCode (),还是有更好的方法来做到这一点?



类似这样的东西是我开始写的,但更好实现将是理想的:

$ p $ class ThreadInfoWrapper
ThreadInfoWrapper(ThreadInfo信息){
this.info = info;
}

//用Thread.State,线程ID等填充实例变量,用
// Getters / setters和所有其他的东西

public boolean equals(Object o){//唯一实现
}
$ b $ public int hashCode(){//无论实现
}

}

但是这种感觉像是一种非常迂回的方式来实现一些基本的功能。我研究了它,并且在Java标准库中不存在具有自定义比较器的集合的实现。我想我可以编写我自己的哈希集实现,但对于简单的情况来说这太多了。通过扩展,你的意思是如何创建派生类,它使用私有构造函数作为派生类他们的超类构造函数。你不能,他们被私下阻止你这样做。由于JRE类是由合格的程序员编写的,因此会有很好的理由。所以,即使你可以使用欺骗来解决它,比如反射或者字节码操作,你也不应该这样做。



但所有的东西都不会丢失。无论如何,你应该更喜欢组合来继承。 Decorator和Proxy设计模式可能很有用(您的示例接近这些)。


Is there a standard technique to "extend" a class with private constructors, as with most Singleton classes? Specifcally, I'm trying to extend the java.lang.management.ThreadInfo class because I'm adding a LOT of them to a HashSet to control uniqueness. However, the way I determine if two threads are equal is different and not identical to the default implementation of the equals() method.

Extending the class is obviously not an option in this case.

Would it be reasonable to make something like a wrapper class which accepts a ThreadInfo in the constructor and then manually populates all relevant fields with the values, then overrides equals() and hashCode(), or is there a better way to do this?

Something like this is what I'm starting to write, but a better implementation would be ideal:

class ThreadInfoWrapper {

    private ThreadInfo info;
    ThreadInfoWrapper(ThreadInfo info) {
        this.info = info;
    }

    //Populate instance variables with Thread.State, thread ID, etc.. with
    //Getters/setters and all that other stuff

    public boolean equals(Object o) { //Unique implementation
    }

    public int hashCode() { //Whatever implementation
    }

}

But this feels like a very roundabout way to achieve some basic functionality. I looked into it, and implementations of Sets with custom Comparators do not exist in the Java standard library. I suppose I could write my own hash set implementation but that's too much work for a simple situation. Any insights would be helpful.

解决方案

By extended, you mean how to create derived classes, which make use of the private constructor as their super class constructor. You can't, they were made private to prevent you doing that. As the JRE classes were written by competent programmers, there will be good reasons for this. So even if you could work around it using trickery, such as reflection or bytecode manipulation, you should not do so.

But all is not lost. You should anyway prefer composition to inheritance. The Decorator and Proxy design patterns can be useful (your example is close to these).

这篇关于用私有构造函数扩展类的技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆