如何实例化一个接口对象?

时间:2015-12-08 15:01:49

标签: java design-patterns

我有一个abstract类和几个实现。

基于实现类,我想执行一个方法并使用new构造函数实现实现类本身。

当然以下内容不起作用,因为课程是Abstract,我无法改变这一事实。但是,如何将“逻辑”如何创建方法prepareList()创建每个实现对象?特别是关于设计决策。

abstract class AbstractClass {
    int id;
    String name;
    int age;
    //lots of fields
}

class FirstClass extends AbstractClass {

}

class SecondClass extends AbstractClass {

}


class MyService {
    void execute(Data data) {
        List<FirstClass> list = prepareList(data, FirstClass.class);
        //process list
    }

    List<AbstractClass> prepareList(Data data, Class<? extends AbstractClass> clazz) {
        List<AbstractClass> collection = new ArrayList<>();

        for (Some value in data.getList()) {
            AbstractClass obj = new AbstractClass(); //does of course not work with abstract class
            obj.setId();
            obj.setName();
            obj.setAge();
            collection.add(obj);
        }

        return collection;
    }
}

2 个答案:

答案 0 :(得分:1)

abstract class Fruit { String name; int weight; }
class Apple extends Fruit {}
class Orange extends Fruit {}

class FruitFactory() {
    public Fruit create(Data data) {
        if ("apple" == data.getFruitName()) return new Apple();
        else if ("orange" == data.getFruitName()) return new Orange();
        else throw new UnsupportedOperationException();
    }
}

class FruitService {

   private FruitFactory factory = new FruitFactory();

   void execute(Data data) {          
       List<Fruit> list = prepareList(data);
   }

   List<Fruit> prepareList(Data data) {
       List<Fruit> fruits = new ArrayList<>();
       for (Some value : data) {
          Fruit fruit = factory.create(data);
          // set fields
          fruits.add(fruit);
       }
       return fruits;
   }
}

答案 1 :(得分:-1)

好的,我有一个建议,为什么你不喜欢

public class AbstractClass {
    int id;
    String name;
    int age;
    //lots of fields


}

class FirstClass extends AbstractClass{

}

class SecondClass extends AbstractClass{

}

class Data{

}

class Myservice{
    void execute(Data data) {
        List<FirstClass> list = prepareList(data, new FirstClass());//notice change
        //process list
    }

    List<AbstractClass> prepareList(Data data, AbstractClass abstractClassObj) {
        List<AbstractClass> collection = new ArrayList<>();

        for (Some value in data.getList()) {
          /*  AbstractClass obj = new AbstractClass();*/ //does of course not work with abstract class
            abstractClassObj.setId();
            abstractClassObj.setName();
            abstractClassObj.setAge();
            collection.add(obj);
        }

        return collection;
    }
}

通过此,您的对象创建和逻辑也将保留在不同的位置。如果将来你有另一个扩展抽象类的类

,它也会有所帮助

或者您可以拥有一些工厂并将工厂对象传递给您的方法或服务类,并在您想要的方法中获取实例。

抱歉匆忙我错过了循环的事情,工厂尝试了一些像

public class AbstractClass {
    int id;
    String name;
    int age;
    //lots of fields

}

class FirstClass extends AbstractClass{

}

class SecondClass extends AbstractClass{

}

class Factory{
    public AbstractClass getInstance( Class<? extends AbstractClass> clazz){
        if(clazz.getSimpleName().equals(FirstClass.class.getSimpleName())){
            return new FirstClass();
        }else {
            return new SecondClass();
        }
    }
}

class Data{

}


class Myservice{

    private final Factory factory;
    public Myservice(Factory factory){
        this.factory = factory;
    }

    void execute(Data data) {
        List<FirstClass> list = prepareList(data, FirstClass.class);//notice change
        //process list
    }

    List<AbstractClass> prepareList(Data data,  Class<? extends AbstractClass> clazz) {
        List<AbstractClass> collection = new ArrayList<>();

        for (Some value in data.getList()) {
            AbstractClass obj = factory.getInstance(clazz);
            abstractClassObj.setId();
            abstractClassObj.setName();
            abstractClassObj.setAge();
            collection.add(obj);
        }

        return collection;
    }
}

显然你可以根据需要修改例子,你可以使用枚举的东西。这只是基本的例子