如何自动填充 java 中的对象?

how to auto-fill an object in java?

我有一个简单的pojo

class animal{

private String name;

animal(String name){
 this.name = name;}

private Features features;

private int max_life;

//
*
* other properties and their getter's and setter's
*//
}

所以现在一旦我用任何 name 初始化 POJO 的名称,我希望其余的属性自动填充。

例如:animal("cat") 应该根据猫自动填充其他属性,例如 max_lifefeatures

是否有任何 属性 的文件或任何方法可以检测初始化并使用预定义的属性自动填充它们??

有一个名为 Podam 的库可以帮助自动填充 Pojo。我在下面提供 link.

https://mtedone.github.io/podam/

引用link,你必须这样使用。

// Simplest scenario. Will delegate to Podam all decisions
PodamFactory factory = new PodamFactoryImpl();

// This will use constructor with minimum arguments and
// then setters to populate POJO
Pojo myPojo = factory.manufacturePojo(Pojo.class);

// This will use constructor with maximum arguments and
// then setters to populate POJO
Pojo myPojo2 = factory.manufacturePojoWithFullData(Pojo.class);

// If object instance is already available,
// Podam can fill it with random data
Pojo myPojo3 = MyFactory.getPojoInstance();
factory.populatePojo(myPojo3);

我其实可以想出几种方法来实现这样的事情。但是你要找的是 工厂.

思路是:工厂接收动物种类,并根据种类创建实例。

总的来说,最基本(虽然很糟糕)的例子是:

public class AnimalFactory {
    public Animal create(String name) {
        if (name.equals(“cat”)) {
            return new Animal(...);
        }
    // other
    }
}

然后您可以这样创建动物:

AnimalFactory factory = new AnimalFactory();
Animal kitty = factory.create(“cat”);

这可以通过多种方式完成,并在许多方面进行改进。详细了解 工厂设计模式

你可以使用 Factory Design Pattern for this, possibly in combination with Strategy Design Pattern.

class Animal{

private String name;

  Animal(String name)
  {
     this.name = name;
     switch(name){
        case "cat":
           this.features = new Features("cat");
           this.max_life = 16;
           break;
        case "dog":
           this.features = new Features("dog");
           this.max_life = 15;
           break;
        default:
           this.features = new Features("unknown");
           this.max_life = 0;
           break;
     }

  }

  private Features features;

  private int max_life;

//
*
* other properties and their getter's and setter's
*//
}

除了使用 Abstract Factory Pattern, which I would recommend, you could also consider using the Prototype Pattern.

您有一些选择:

1 - 在构造函数中初始化

class Animal {

   private String maxLife;
    
   public Animal (String name) {
      switch(name) {
         case "cat":
            maxLife = 10;
            break;
         case "dog":
            maxLife = 20;
            break;
         default:
           maxLife = 1;
      }
   }
}

2 - 使用继承:

abstract class Animal {
   String name;
   int maxLife;
}

class Cat extends Animal {
  public Cat() {
     maxLife = 10;
  }
}

class Dog extends Animal {
  public Dog() {
     maxLife = 20;
  }
}

3 - 使用工厂(选项 2 的 类):

class AnimalFactory {
   
   public static Animal create(String name) {
       switch(name) {
         case "cat":
            return new Cat();
         case "dog":
            return new Dog();
      }
   }

}

此外,在 Java 中,惯例是使用 CamelCase。 类需要大写,variables/fields需要小写