Kjensen
Kjensen

Reputation: 12384

Implementing rebate

Classic scenario, where an order has order lines.

The client wants to be able to apply a rebate to the entire order, that is either a fixed amount or a percentage.

What would be the best way to implement that?

I am thinking storing two fields on the order-object:

And then I can calculate the total rebate - and make the rebate an order line, that I re-calc every time the rebate-fields or order lines are changed.

Caveats, hints, best practices?

Upvotes: 1

Views: 151

Answers (3)

Yaroslav Yakovlev
Yaroslav Yakovlev

Reputation: 6483

Did you think of adding rebates to all order lines. If no rebate - it`s a zero rebate. Any rebate can be added.

Pattern you should use in this case depends on how are you going to use rebates. If single rebate on the order - strategy is very well.

If rebates can work with each other (rebate for first buy, 10000 client and discount card) you should look at decorator as other way you`ll be have to implement as much strategies as rebates combination.

Upvotes: 1

Vinay Sajip
Vinay Sajip

Reputation: 99495

There's more flexibility in keeping the rebate logic separate from the order. For example, business rules might change to have a more flexible rebate strategy, e.g. rebates only apply to some order lines based on whether there is a promotion for those items. That's more akin to a strategy pattern (which might or might not be overkill for your scenario and business environment).

In that scenario, the order would have a foreign key into a rebate table which identified the rebate strategy and any associated data such as percentage or fixed amount.

Upvotes: 2

BobBrez
BobBrez

Reputation: 723

Take a look into using something like the composite pattern.

The rebates should not be stored within the objects themselves but should be applied as an external entity. Think about providing enough flexibility for realistic changes.

Upvotes: 2

Related Questions