[英]how to auto-fill an object in java?
i have a simple 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
*//
}
so now once i initialize the name of the POJO with any name
i want the rest of the attributes to auto fill. 所以现在一旦我用任何
name
初始化POJO的name
我都希望其余属性自动填充。
eg: animal("cat")
should auto-fill other attributes such as max_life
and features
based on a cat. 如:
animal("cat")
应自动填写其他属性,如max_life
和features
基础上一只猫。
is there any property's file or any way that will detect the initialization and auto fill them with pre-defined properties?? 是否有任何属性的文件或任何方式可以检测到初始化并自动用预定义的属性填充它们?
You have some options: 您有一些选择:
1 - Initializing in the constructor 1-在构造函数中初始化
class Animal {
private String maxLife;
public Animal (String name) {
switch(name) {
case "cat":
maxLife = 10;
case "dog":
maxLife = 20;
default:
maxLife = 1;
}
}
}
2 - Using Inheritance: 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 - Using a factory (with classes of option 2): 3-使用工厂(具有选项2的类别):
class AnimalFactory {
public static Animal create(String name) {
switch(name) {
case "cat":
return new Cat();
case "dog":
return new Dog();
}
}
}
Also, in Java, the convention is to use CamelCase. 另外,在Java中,约定是使用CamelCase。 For classes, it should be capitalized and for variables/fields should start with lowercase.
对于类,应将其大写,对于变量/字段,应以小写字母开头。
There is a library called Podam which helps in auto filling a Pojo. 有一个名为Podam的库,可以自动填充Pojo。 I provide below the link.
我在链接下方提供。
https://mtedone.github.io/podam/ https://mtedone.github.io/podam/
To quote from the link, you have to use like this. 要引用链接,您必须像这样使用。
// 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);
I can actually think of several ways to implement such thing. 实际上,我可以想到几种实现这种方法的方法。 But what you're looking for is a factory .
但是,您要寻找的是工厂 。
The idea is: the factory receives the animal kind, and creates the instance based on the kind. 这个想法是:工厂接收动物的种类,并根据种类创建实例。
In general, the most basic (although terrible) example is: 通常,最基本(尽管很糟糕)的示例是:
public class AnimalFactory {
public Animal create(String name) {
if (name.equals(“cat”)) {
return new Animal(...);
}
// other
}
}
You may then create animals as such: 然后,您可以像这样创建动物:
AnimalFactory factory = new AnimalFactory();
Animal kitty = factory.create(“cat”);
This can be done in multiple ways and improved in many aspects. 这可以通过多种方式完成,并且可以在很多方面进行改进。 Read more about the Factory design pattern .
阅读有关Factory设计模式的更多信息。
您可以为此使用Factory Design Pattern ,也可以与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
*//
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.