【问题标题】:how to auto-fill an object in java?如何在java中自动填充对象?
【发布时间】:2019-09-04 18:56:45
【问题描述】:

我有一个简单的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

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

【问题讨论】:

  • 我会在构造函数中初始化默认值
  • 这对其他值如何起作用,例如:狗、鼠标??
  • if (name == "cat") { ... } else if (name == "dog") { ... } else if (name == "mouse") { ... }?
  • to many if else,看到我可以有许多可能需要默认值的值。
  • 使用 switch 语句或定义抽象类/接口。您正在寻求一种更好的方法来做一些实际上没有更好解决方案的事情。

标签: java pojo


【解决方案1】:

你有一些选择:

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。对于类,它应该大写,变量/字段应该以小写开头。

【讨论】:

  • 您的第一个示例应在每个案例之后包含break 语句。
  • 谢谢,刚刚做了。 “解决方案”的想法保持不变:)
【解决方案2】:

我实际上可以想到几种方法来实现这样的事情。但是您正在寻找的是一个工厂

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

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

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

这可以通过多种方式完成,并在许多方面进行改进。阅读有关工厂设计模式的更多信息。

【讨论】:

    【解决方案3】:

    有一个名为 Podam 的库可以帮助自动填充 Pojo。我在链接下方提供。

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

    要引用链接,你必须像这样使用。

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

    【讨论】:

      【解决方案4】:

      您可以为此使用Factory Design Pattern,可能与Strategy Design Pattern 结合使用。

      【讨论】:

        【解决方案5】:
        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
        *//
        }
        

        【讨论】:

          【解决方案6】:

          除了我推荐的Abstract Factory Pattern,您还可以考虑使用Prototype Pattern

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-17
            相关资源
            最近更新 更多