jeudi 10 octobre 2019

What pattern can be used to create an object of one class, but fill its properties in different ways?

I have a class like this

public class OwnerWithholding
    {

        private decimal ManagementFeePct;

        private decimal TotalManagementFee;

        private decimal OperationalFeesPct;

        private decimal TotalOperationalFees;

}

And I have method that create object of this class, fill it with some arithmetic operations, and return this object.

public OwnerWithholding CheckoutFeeDistribution(Reservation r, SqlConnection conn)
        {
            OwnerWithholding result = new OwnerWithholding();

           // result.ManagementFeePct  = some data from Fees table in DB + value 
           //from another db - constant value..
           // Also with other properties - with some operations on data

            return result;
        }

And now it works fine. But this method is just one option for populating data.

There is another method, implemented in a completely different way, but filling exactly the same properties of the object. And I may have more of them.

I need a pattern that would allow me to create objects of the same class, just indicating the calculation option that is needed. I like the strategy pattern , where the algorithms will be the methods that fill the objects that called them. But it doesn’t look very good. Maybe a factory method would be more appropriate here, but I don’t know how to implement it.

Aucun commentaire:

Enregistrer un commentaire