接口和工厂模式的问题

时间:2015-04-09 23:31:23

标签: java inheritance abstract-class factory-pattern

我创建了一个工厂模式来创建我的对象,但是每个对象都略有不同,我希望它们实现不同的方法,我当前的设置我必须在接口和后续对象类中声明所有方法,我怎么能绕过这个?

这是结构的一个例子

食品类

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);

我删除了任何unessary代码以节省空间,我试图做的是将食物和草食动物存储为数组中的活动对象,但我希望能够在每个上调用不同的方法,我无法将它们更改为摘要因为继承而上课所以所有人都有想法,有人可以帮忙吗?

提前谢谢

1 个答案:

答案 0 :(得分:1)

也许我误解了你的问题,但为什么你不能有这样的事情呢? 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方法只包含一个print语句,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();
}

班级EntityMyObjects为空。 DrinkFood延长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." );
    }
}

CarnivoreHerbivore延长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{};