Java 事务可以是类吗?

Java 事务可以是类吗?,java,c#,oop,object-oriented-analysis,Java,C#,Oop,Object Oriented Analysis,昨天我在采访中被问到,“交易能成为一门课吗?” 我说,“是的”。他回答的是类还是函数 我回答说,如果它有一个非常简单的功能,它可以是一个函数,但它应该是一个类 他说,好吧,让我们暂时接受它,因为它可以成为一门课。编写它的代码 我写道: class transaction{ int timestamp; Account acc; int type; public transaction(int timestamp,int type,Account acc){ this.time

昨天我在采访中被问到,“交易能成为一门课吗?”

我说,“是的”。他回答的是类还是函数

我回答说,如果它有一个非常简单的功能,它可以是一个函数,但它应该是一个类

他说,好吧,让我们暂时接受它,因为它可以成为一门课。编写它的代码

我写道:

class transaction{
 int timestamp;
 Account acc;
 int type;

 public transaction(int timestamp,int type,Account acc){
     this.timestamp = timestamp; this.int = int ; this.acc =acc;
 }

 public withdraw ( double amount){
     acc.amount -= amount;
 }

 //And other type of transaction function, like add money etc.

} 

让我知道我的回答是对的还是错的,我从他的表达中看不出多少。

交易是一个名词,因此它应该被建模为一个对象。消息(或就此而言,函数)是动词,因此非常适合执行操作。
事务
(名词)可以
开始
(动词)、
结束
(动词)或
中止
(动词),并且应该完全
还原
ed(动词)或
提交
ted(动词)

编辑

@阿列克谢列文科夫在评论部分的评论正确地指出,
撤销
不是一条正确的
交易
信息。
draw
操作的正确消息是(Smalltalk语法)

进行
提取:在:
适当的
账户
操作(方法)。鉴于
事务的一般性质,它应该在

aTransaction do: aBlock
    aTransaction start
    [aBlock value] ifCurtailed: [aTransaction abort].
    aTransaction commit
这样我们就可以

anAccount withdraw: anAmount in: aTransaction
    aTransaction do: [anAccount withdraw: anAmount]

事务
对象的有趣之处在于它能够捕获(并建模)基本协议
开始
提交
中止
,以及方法
do:
,用于一般操作的一般评估(在我的编码中由Smalltalk块表示)

我可能会这样写:

public interface IRecord
{
    public DateTime TimeStamp {get;set;}
}

public enum TransactionType 
{
    Debit , Credit
}

public class Transaction : IRecord
{
    public DateTime TimeStamp {get;set;}
    public decimal Amount {get;set;}
    public TransactionType Type {get;set;}
    public Account TargetAccount {get;set;} 
}

public class Atm 
{
    private List<Tuple<Acount,Transaction>> _record = new List<Tuple<Account,Transaction>>();
    private ICashHandler _cashHandler;

    public Atm(ICashHandler handler)
    {
        _cashHandler = handler;
    }

    public void Withdraw(Account account, decimal amount)
    {
        _record.Add(Tuple.Create(account, 
            new Transaction {
                TimeStamp = DateTime.Now,
                Amount = amount,
                Type = TransactionType.Debit,
                TargetAccount = account
            })
        );

        _cashHandler.Dispense(amount);
    }

    public void Deposit(Account account)
    {
        decimal amount;

        if( _cashHandler.TakeDeposit( out amount ) ) 
        {

            _record.Add(Tuple.Create(account, 
                new Transaction {
                    TimeStamp = DateTime.Now,
                    Amount = amount,
                    Type = TransactionType.Credit,
                    TargetAccount = account
                })
            );
        }

    }

}
公共接口IRecord
{
公共日期时间时间戳{get;set;}
}
公共枚举事务类型
{
借贷
}
公共类事务:IRecord
{
公共日期时间时间戳{get;set;}
公共十进制数{get;set;}
公共事务类型{get;set;}
公共帐户TargetAccount{get;set;}
}
公共级自动取款机
{
私有列表_记录=新列表();
私人ICashHandler(现金处理人);
公共Atm(ICashHandler)
{
_现金处理程序=处理程序;
}
公共作废取款(账户,小数金额)
{
_record.Add(元组)和Create(帐户、,
新交易{
时间戳=日期时间。现在,
金额=金额,
类型=交易类型。借方,
TargetAccount=帐户
})
);
_现金处理员。分配(金额);
}
公共无效存款(账户)
{
小数金额;
如果(_cashHandler.接受存款(取出金额))
{
_record.Add(元组)和Create(帐户、,
新交易{
时间戳=日期时间。现在,
金额=金额,
类型=交易类型。信用,
TargetAccount=帐户
})
);
}
}
}
只是一个简单的例子,应该有帐户金额等检查


其思想是,事务是一个记录,发生的事务的操作将是方法(在本例中由Atm类处理)。transaction类不应该有取款和存款之类的操作,它只是帐户上操作的简单记录,而不是实际操作。

是的,您是对的。希望提问的人也受过良好的OOP教育。@LeandroCaniglia我想说“撤回”不是原始示例中所示的事务对象操作,而是事务类型。。。我真的怀疑OP是否和面试官想要的接近。
public interface IRecord
{
    public DateTime TimeStamp {get;set;}
}

public enum TransactionType 
{
    Debit , Credit
}

public class Transaction : IRecord
{
    public DateTime TimeStamp {get;set;}
    public decimal Amount {get;set;}
    public TransactionType Type {get;set;}
    public Account TargetAccount {get;set;} 
}

public class Atm 
{
    private List<Tuple<Acount,Transaction>> _record = new List<Tuple<Account,Transaction>>();
    private ICashHandler _cashHandler;

    public Atm(ICashHandler handler)
    {
        _cashHandler = handler;
    }

    public void Withdraw(Account account, decimal amount)
    {
        _record.Add(Tuple.Create(account, 
            new Transaction {
                TimeStamp = DateTime.Now,
                Amount = amount,
                Type = TransactionType.Debit,
                TargetAccount = account
            })
        );

        _cashHandler.Dispense(amount);
    }

    public void Deposit(Account account)
    {
        decimal amount;

        if( _cashHandler.TakeDeposit( out amount ) ) 
        {

            _record.Add(Tuple.Create(account, 
                new Transaction {
                    TimeStamp = DateTime.Now,
                    Amount = amount,
                    Type = TransactionType.Credit,
                    TargetAccount = account
                })
            );
        }

    }

}