jeudi 23 juillet 2015

Handle transaction maagment using Factory and Singelton pattern java

I need some help regarding design of a mini point of sale application. In general we could do sale and refund transaction. During one instance of the pos, there should be only one transaction. Once that transaction is finished then some other transaction could be start (e.g: a new sale transaction or refund transaction e.t.c). The problem that i am facing is that i need one point of entry or manager to manage the transaction in the pos. But i am stuck in the design pattern.

What i have reached so far is that. We have a transaction interface like this:

Interface ITransaction
{
    getTransacitonId();
    setTransacitonID();
    ... //some other basic methods.
}
abstarct BasicTransaction implements ITransaction
{
   //has some basic implementaion of interface methods and some other stuff.
  protetced Vector itemLines = null; 
  addItemLine()
  removeItemLine()      
}
SaleTransaction extends BasicTransaction
{
   //some customized properties for a sale transaction
}
RefundTransaction extends BasicTransaction
{
  //some customized properties for a refund transaction
}

This is the transaction hierarchy. The real problem occurs in managing this transaction. I have created a Transaction Manager for this.

class TransactionManager
{
    public SaleTransacation createSaleTransaction();
    public RefundTransaciton createRefundTransaction();
    public boolean finishTransaction(ITransaction);
    public boolean serializeTransactionOnDisk(ITransaction);
}

But the problem is that we could have more then one transaction manager in the application. Any one can do that, so i created a TransactionManagerFactory to avoid that and also make it singelton, so we could not have more then one TransacitonManagerFactory. Its code is given below.

TransactionManagerFactory
{
    private TransactionManager transactionManger;
    static private TransactionManagerFactory factory;

    private TransactionManagerFactory()
    {
        transactionManger = new TransactionManager();
    }

    public static TramsactionManager getTransactionManager(){
        if(factory == null) 
            factory = new TransactionmanagerFactory();
        return transactionManager;  
    }
}

So the first problem would be that at any given point we could instantiate more the one kind of transaction from TransactionManager. Although there is only one Transaction Manager but it didn't solve that problem. Or should i make it a factory also.

And the second problem is what stops me / programmer for directly instantiating the saleTransaction object. (I can do that , couldn't I?), so why go all the trouble of going through transaction factory --> and then transaciton manager --> and then SaleTransaction, if it didn't solve the problem

Please suggest me some flaws and the best approach to solve this problem.

Aucun commentaire:

Enregistrer un commentaire