向Java类库添加函数 [英] Adding functions to Java class libraries
问题描述
我使用的Java类库在许多方面是不完整的:有很多类,我觉得应该有内置的额外的成员函数。但是,我不确定添加这些成员函数的最佳实践。 / p>
允许调用不足的基类 A
。
class A
{
public A(/ * long任意参数* /)
{
// ...
}
public A(/ * long even arbitrary arguments * /)
{
// ...
}
public int func()
{
return 1;
}
}
理想情况下,我想添加一个函数到 A
。但是,我不能这样做。我的选择是:
class B extends A
{
//在这里实现A的所有构造函数
public int reallyUsefulFunction()
{
return func()+ 1;
}
}
和
class AddedFuncs
{
public static int reallyUsefulFunction(A a)
{
return a.func ;
}
}
我看到它的方式,缺点。第一个选择给出了比第二个更清晰的语法,并且更合乎逻辑,但有问题:假设我在类库中有一个第三个类 C
p>
class C
{
public A func()
{
return new A (/*...*/);
}
}
正如我看到的,没有简单的方法这样做:
C c;
int useful = c.func()。reallyUsefulFunction(); code> C.func()返回的类型$ c $ <$> $ c $ b
c>是A
,而不是B
,您不能下载。
那么,什么是将成员函数添加到只读库类的最好方法?
解决方案自然和频繁的困境。请阅读 组合对继承 替代方法。如果我们认为对象A在构造函数中传递,而不是在每个方法中传递,那么你的第二个选择基本上是一个组合。也就是说,我们使用组合来实现包装或 装饰 模式。
类C的返回一个新的类A的实例的问题没有平凡的解决方案,因为你猜测,只要C类决定采取负责任的创建新的实例。这就是为什么要在类中键入一个新语句之前暂停和思考,如果这个类有可能被子类化。在yout示例中,如果你能告诉类C返回什么具体类,那么这将是很好的,但是它如何知道创建它呢?好了,我们可以传递一个对象,他知道如何实例化一个类的对象(或一个子类)...我想你有足够的动力阅读 工厂 和一般的设计模式。
答案,但如果想要一个快速:我会做一个包装,B类不扩展A,但有一个构造函数与A作为参数,它委派其方法(除了自己)到内部对象。
当你需要调用类C中的方法(我假设你不能碰类C),你可以写:B b = new B(c.func())
I'm using a Java class library that is in many ways incomplete: there are many classes that I feel ought to have additional member functions built in. However, I am unsure of the best practice of adding these member functions.
Lets call the insufficient base class
A
.class A { public A(/*long arbitrary arguments*/) { //... } public A(/*long even more arbitrary arguments*/) { //... } public int func() { return 1; } }
Ideally, I would like to add a function to
A
. However, I can't do that. My choice is between:class B extends A { //Implement ALL of A's constructors here public int reallyUsefulFunction() { return func()+1; } }
and
class AddedFuncs { public static int reallyUsefulFunction(A a) { return a.func()+1; } }
The way I see it, they both have advantages and disadvantages. The first choice gives a cleaner syntax than the second, and is more logical, but has problems: Let's say I have a third class,
C
, within the class library.class C { public A func() { return new A(/*...*/); } }
As I see it, there is no easy way of doing this:
C c; int useful = c.func().reallyUsefulFunction();
as the type returned by
C.func()
is anA
, not aB
, and you can't down-cast.So what is the best way of adding a member function to a read-only library class?
解决方案Natural and frequent dilemma. Read about the composition vs inheritance alternative. Your second alternative is basically a composition, if we think that the object A is passed in the constructor instead of passing it in each method - that is, we would be using composition to implement a wrapper or decorator pattern.
The issue for class C returning a new instance of class A has no trivial solution, as you guessed, as long as class C decides to take responsability of creating the new instance. This is why one should pause and think before typing a "new" statement inside a class, if there is the possibility that this class will be subclassed. In yout example, it would be nice if you could tell class C what concrete class to return ... but how would it know to create it? Well we could pass him an object who knows how to instantiate an object of class A (or a subclass)... I guess you are enough motivated to read about Factories now, and design patterns in general.
There is no unique best answer, but if want a quick one: I'd make a wrapper, B class does not extend A but has a constructor with A as parameter, it delegates its methods (except the own) to the inside object. When you need to call the method in class C (I'm assuming you cant touch class C), you could write:
B b = new B(c.func())
这篇关于向Java类库添加函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!