我已经创建了一个工厂模式来创建对象,但是每个对象都略有不同,并且我希望它们实现不同的方法,在当前设置下,我必须在接口和后续对象类中声明所有方法,我能解决这个问题吗?
这是结构的一个例子
食品课
public class Food extends MyObjects implements MyActiveObject, {
public Food(String name, Coordinates coordinates, int size, Color color) {
super(name, coordinates, size, color);
}
}
草食动物类
public class Herbivore extends Entity implements MyActiveObject {
public Herbivore(String name,Coordinates coordinates, int size, Color color){
super(name, coordinates, size, color);
}
}
接口
public interface MyActiveObject extends EntityInterface {
public Coordinates getCoordinates();
etc...
}
工厂级
public class MyActiveObjectFactory {
public MyActiveObject createObject(String objectType, int objectCount, int x, int y, int scale){
if(objectType == null){
return null;
}else{
if(objectType.equalsIgnoreCase("HERBIVORE")){
return new Herbivore();
}
else if(objectType.equalsIgnoreCase("CARNIVORE")){
return new Carnivore();
}
else if(objectType.equalsIgnoreCase("FOOD")){
return new Food();
}
else if(objectType.equalsIgnoreCase("DRINK")){
return new Drink();
}
}
return null;
}
对象创建
MyActiveObject activeObject = activeObjectFactory.createObject("Herbivore", activeObjectCount, x, y, scale);
香港专业教育学院删除了任何不必要的代码以节省空间,我想做的是将食物和草食动物作为活动对象存储在数组中,但是我希望能够对它们中的每一个调用不同的方法,由于以下原因,我无法将它们更改为抽象类:这样的继承是我的全部想法,任何人都可以帮忙吗?
先感谢您
最佳答案
也许我误会了您的问题,但是为什么您不能拥有这样的东西? getCoordinates
方法在接口中定义,并在每个类中实现,但是每个类也具有自己的方法。
public class Test{
private MyActiveObject[] mObjList = new MyActiveObject[ 4 ];
public Test(){
mObjList[0] = new Food();
mObjList[1] = new Drink();
mObjList[2] = new Herbivore();
mObjList[3] = new Carnivore();
}
public void doTest(){
for( MyActiveObject obj : mObjList ){
obj.getCoordinates(); // No conversion required for common method
if( obj instanceof Herbivore ){
Herbivore newObj = ( Herbivore ) obj;
newObj.doHerbivoreWork();
}
else if( obj instanceof Carnivore ){
Carnivore newObj = ( Carnivore ) obj;
newObj.doCarnivoreWork();
}
else if( obj instanceof Food ){
Food newObj = ( Food ) obj;
newObj.doFoodWork();
}
else if( obj instanceof Drink ){
Drink newObj = ( Drink ) obj;
newObj.doDrinkWork();
}
}
}
public static void main( String[] args ){
new Test().doTest();
}
}
do<class>Word
方法与getCoordinates
方法一样仅包含一条打印语句。运行上面的输出是:Getting Food Coordinates...
Working on Foods.
Getting Drink Coordinates...
Working on Drinks.
Getting Herbivore Coordinates...
Working on Herbivores.
Getting Carnivore Coordinates...
Working on Carnivores.
更新:这是接口的定义:
public interface MyActiveObject{
public Coordinates getCoordinates();
}
类
Entity
和MyObjects
为空。 Drink
和Food
扩展MyObjects
:public class Drink extends MyObjects implements MyActiveObject{
@Override
public Coordinates getCoordinates(){
System.out.println( "Getting Drink Coordinates..." );
return new Coordinates();
}
public void doDrinkWork(){
System.out.println( "Working on Drinks." );
}
}
Carnivore
和Herbivore
扩展Entity
:public class Carnivore extends Entity implements MyActiveObject{
@Override
public Coordinates getCoordinates(){
System.out.println( "Getting Carnivore Coordinates..." );
return new Coordinates();
}
public void doCarnivoreWork(){
System.out.println( "Working on Carnivores." );
}
}
更新第二个:
我想我看到了问题。您使用的接口具有很多类中不需要的方法签名。是吗
在这种情况下,仅创建一个空的标记接口只是为了能够在数组中放置不同的类。
public interface Arrayable{};
关于java - 接口(interface)和工厂模式问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29550823/