lundi 7 novembre 2016

Using Stack Allocation instead of Heap allocation for factory methods

I am trying to refactor some code. Currently that code follows pattern like :

function f() {
Transaction trans;
// Do Operation
trans.commit();
}

Here transaction is concrete class. I want to move it to Interface and make this code look like as:

function f() {
ITransaction* trans = GetTransaction();
// Do Operation
trans->commit();
}

// Factory 
ITransaction* GetTransaction()
{
return new Transaction();
}

PROBLEM: we started using new here in factory method, which make heap allocation now. I want to avoid such heap allocation and still manage to use Interface. Is there any way to achieve this?

Thanks, Kailas

Aucun commentaire:

Enregistrer un commentaire