问题描述
假设我在 ProjectA
中有一个名为 Human
的类。它在同一个项目的 CreatureBuilder
类中实例化。 现在我要创建一个新的类code> Cyborg 在另一个项目中,即在 ProjectB
中。 ProjectB
在其导入中具有 ProjectA
,但 ProjectA
约 ProjectB
。
Cyborg扩展人类
,必须也由 CreatureBuilder
ProjectA
(所以 Cyborg
位于 ProjectB
中,我从 ProjectB
中调用 CreatureBuilder
实例化 Cyborg
,但 CreatureBuilder
位于 ProjectA
中,作为以及我的人类
类)。
我需要一个逻辑来创建一个 / code>当
CreatureBuilder
从 ProjectA
中实例化,并创建一个 Cyborg
当 CreatureBuilder
从 ProjectB
中实例化。
我认为可以通过在 ProjectA
中创建一个 getCreature()
方法的界面来实现。该方法将在 ProjectB
中被覆盖到返回新的Cyborg
,然后传回 CreatureBuilder
ProjectA
。
任何其他建议?你认为最好的解决方法是什么?我可以使用反思API吗?
干杯!
Java 8
CreatureBuilder可以将生物的创建委托给呼叫者,要求一个。
public class CreatureBuilder {
public Creature getCreature(Supplier< Creature> creatureSupplier)
{
//做一些未知的东西
return creatureSupplier 。得到();
}
}
ProjectA的使用
public class ProjectA {
public static void main(String [] args){
生物A = new CreatureBuilder()。getCreature ::新);
}
}
ProjectB的用法
public class ProjectB {
public static void main(String [] args){
生物B = new CreatureBuilder()。getCreature(Cyborg ::新);
}
}
如果没有被迫使用, 。
Java 7
如果你坚持使用Java 7,原则是一样的,除了它有点更冗长。
您必须声明并使用您自己的供应商界面
public interface CreatureSupplier {
Creature get();
}
public class CreatureBuilder {
public Creature getCreature(CreatureSupplier creatureSupplier)
{
//做一些事情
return creatureSupplier.get ();
}
}
使用情况有点更详细
public class ProjectA {
public static void main(String [] args){
生物A = new CreatureBuilder()。 getCreature(new CreatureSupplier(){
@Override
public Creature get(){
return new Human();
}
});
}
}
public class ProjectB {
public static void main(String [] args){
生物B = new CreatureBuilder()。getCreature (new CreatureSupplier(){
@Override
public Creature get(){
return new Cyborg();
}
});
}
}
而且就是这样,你有与Java 8中的行为相同。
Let's say I have a class named Human
in ProjectA
. It is instantiated in the CreatureBuilder
class of the same project.
Now I want to create a new class called Cyborg
in a different project, i.e. in ProjectB
. ProjectB
has ProjectA
in its imports, but ProjectA
knows nothing about ProjectB
.
Cyborg extends Human
, and must also be instantiated by CreatureBuilder
of ProjectA
(so, Cyborg
is located in ProjectB
, I call CreatureBuilder
from ProjectB
to instantiate Cyborg
, but CreatureBuilder
is located in ProjectA
, as well as my Human
class).
I need a logic to create a Human
when CreatureBuilder
is instantiated from ProjectA
, and to create a Cyborg
when CreatureBuilder
is instantiated from ProjectB
.
I think it is achievable by creating an interface with a getCreature()
method in ProjectA
. This method will be overridden in ProjectB
to return new Cyborg
and then passed back to CreatureBuilder
of ProjectA
.Any other suggestions? What do you think is the best workaround? Can I use reflection API instead?
Cheers!
Java 8
CreatureBuilder
can delegate the creature's creation to the caller by asking for a Supplier.
public class CreatureBuilder {
public Creature getCreature(Supplier<Creature> creatureSupplier)
{
//do some unknown things
return creatureSupplier.get();
}
}
Usage from ProjectA
public class ProjectA {
public static void main(String[] args) {
Creature A = new CreatureBuilder().getCreature(Human::new);
}
}
Usage from ProjectB
public class ProjectB {
public static void main(String[] args) {
Creature B = new CreatureBuilder().getCreature(Cyborg::new);
}
}
And never use reflection if you're not forced to.
Java 7
If you're sticked with Java 7, the principle remains the same except that it's a bit more verbose.
You have to declare and use your own Supplier-like interface
public interface CreatureSupplier {
Creature get();
}
public class CreatureBuilder {
public Creature getCreature(CreatureSupplier creatureSupplier)
{
//do some things
return creatureSupplier.get();
}
}
Usage is a bit more verbose
public class ProjectA {
public static void main(String[] args) {
Creature A = new CreatureBuilder().getCreature(new CreatureSupplier() {
@Override
public Creature get() {
return new Human();
}
});
}
}
public class ProjectB {
public static void main(String[] args) {
Creature B = new CreatureBuilder().getCreature(new CreatureSupplier() {
@Override
public Creature get() {
return new Cyborg();
}
});
}
}
And... that's it, you've got the same behaviour as the one in Java 8.
这篇关于实例化扩展类或其父级(取决于具体情况)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!