2016-03-23 19 views
0

我有一个名为Transactions的java类,有几个方法。特别是,在Sqlite数据库中插入代表新交易的所有值,如下所示:多参数还是有什么办法强制使用setter(Java)?

Date;时间;类别;收款人;付款人;值;标签;说明(...)

当我打电话的方法来保存一个新的事务,它看起来像:

new Transactions().saveNewTransaction(String date, String time, String category, Int Payee, Int Payer, String value, String tags, String Description (...) 

我觉得这个方法似乎大了,不好可读的代码和生活的最好方式将这些字段作为来自Transactions类和方法saveNewTransaction()的变量,不接受参数,而是访问类中的变量。

唯一的问题是:如何强制一个类(在我的情况下一个Activity类)调用所有需要保存新事务的设置器?

风险就会调用saveNewTransaction(),并没有被Activity类设定值几个领域(至少在方法保证所有字段必须由调用设置)

谢谢!

+0

你可以有一个属性类与你想要保存的值相同的类,然后通过传递新创建的类的实例来创建一个像saveNewTransaction(object)这样的方法.. –

回答

0

如果你的要求是每个创建的事务对象应该导致一些数据库条目,你可以考虑删除的saveNewtTransaction方法,而是一些“建设者”类中执行保存动作:

public class TransactionBuilder { 
private Date date; 

public TransactionsBuilder with(@NonNull Date date){ 
    this.date = date; 
    return this; 
} 

public Transaction build(){ 
    validateFields(); 

    Transaction transaction = new Transaction(); 
    transaction.set(date); 

    createADatabaseEntry(transaction); 
    return transaction; 
} 

private void validateFields() { 
    org.springframework.util.Assert.notNull(date, "The date cannot be null."); 
} 

checker framework发布编译器警告,当开发人员尝试将null传递给使用@NonNull注释的setter时。

显然,在你的情况下,构建器会和() - 方法有一些不同,也就是一些与你的Transactiion对象/ saveNewTransaction()方法需要的匹配。

Ps我还没有考虑过你的交易类是什么。但是,如果Transactions类只有一种方法,我会创建一个上述解决方案的变体:

  1. 使用定义功能的单个方法创建接口。
  2. 创建一个包含saveNewTransactions()方法逻辑的实现。唯一的区别是这个方法应该只接受一个参数,它是一个“输入bean”,FXx TransactionInputBean。
  3. 创建您输入的bean(一个简单的私人领域的类和公共的getter & setter方法。
  4. 创建输入豆建设者。
0

,你可以把所有这些变量初始化或将它们设置为这不应该由制定者出现值(例如,-1)。

然后在你new Transactions().save();你需要检查他们是否仍然有这个价值。

但这种解决方案将无法工作, w您上面写过,因为您在创建对象时已经保存了事务。在这里你需要先创建新的对象然后调用所有的setter。

你的交易类可以是这样的:

package de.jeanma.stackOverflow; 

import java.lang.Integer; 
import java.lang.String; 

public class Transactions{ 
    private String date, time, category, value, tags, description; 
    private int payee, payer; 

    public Transactions(){ 
    //Add everything here, needed for the constructor 
    } 
    public Transactions(String date, String time, String category, int payee, //I recommend creating an constructor that sets the values as well, beacause you 
     int payer, String value, String tags, String Description){ // might want to create the object and call the save() directly withoud calling every setter one by one   
     this.date = date; 
     this.time = time; 
     this.category = category; 
     this.value = value; 
     this.tags = tags; 
     this.description = description; 
     this.payee = payee; 
     this.payer = payer; 
     //Add everything here, needed for the constructor as well 
    } 

    //Here you can place all your other methods 

    public void save(){ 
     if(!(isInitialized(date) && isInitialized(time) && isInitialized(category) && isInitialized(value) //here all values are checked if they are initialized. 
      && isInitialized(tags) && isInitialized(description) && isInitialized(Integer.valueOf(payee)) //The primitive int's are made to complex Integer's because 
      && isInitialized(Integer.valueOf(payer)))){             // the method isInitialized() expects an object 
     //here you could throw an exception or do something like: System.exit(-1); 
     } 
    } 
    private boolean isInitialized(Object Obj){ // this is the method that's checking if the value is initialized 
     if(Obj.equals(null)) return false; 
     else return true; 
    } 

    //Add all the setters here (I'm too lazy to do that now) 

} 

我希望这awnser满足你。

相关问题