Start learning free

LESSON 322 OF 343 · DESIGN PATTERNS

Strategy Pattern in Rust

Practice Behavioral

Some jobs can be done in several ways, like pricing a cart with different discounts. The Strategy pattern puts each way in its own struct, behind one shared trait. The code that uses them can swap one for another, and it never checks which kind it holds.

Here you build a shop's checkout. Its discount is the strategy, and it can change while the cart fills up.

The trait Discount has one method, apply(total, items). It returns the price to pay for a cart. The cart's sum in cents is total. The number of items in it is items. A trait lists the methods a type promises to have.

Write four structs that implement it:

  • NoDiscount pays total.
  • PercentOff::new(percent) takes percent percent off. The discount is rounded down, so the price is rounded up.
  • AmountOff::new(amount) takes amount cents off. The price never goes below 0.
  • BulkDiscount::new(min_items, percent) pays like a PercentOff of percent when the cart has at least min_items items. A smaller cart pays total.

Then write Checkout:

  • Checkout::new() starts with an empty cart and a NoDiscount.
  • add(price) puts one item of price cents in the cart.
  • set_discount(discount) uses discount from now on.
  • total() returns the price to pay. It asks the current discount's apply.
  • clear() empties the cart and keeps the discount.

Rust has no classes. Each discount and Checkout is a struct, and new makes one. NoDiscount has no fields, so its name alone is the value. Checkout keeps its discount in a Box<dyn Discount>. That box can hold any struct that implements Discount.

The tests make each discount themselves and pass it in, as in set_discount(Box::new(PercentOff::new(10))). So Checkout must call apply through the trait, whatever kind it holds.

Only total() writes output: one line with the price.

 

Example 1:

Input:
Checkout::new()
1. add(800)
2. add(450)
3. total()
4. set_discount(Box::new(PercentOff::new(10)))
5. total()
6. add(199)
7. total()
8. set_discount(Box::new(NoDiscount))
9. total()
Output:
1250
1125
1305
1449
Explanation:
The cart starts with no discount, so call 3 returns 800 + 450 = 1250.
Call 4 swaps in 10 percent off. 10 percent of 1250 is 125, so call 5 returns 1125.
Then the cart holds 1449 cents. 10 percent of that is 144.9, rounded down to 144.
So call 7 returns 1449 - 144 = 1305.
Call 8 swaps back to no discount, and call 9 returns the full 1449.

Example 2:

Input:
Checkout::new()
1. set_discount(Box::new(BulkDiscount::new(3, 20)))
2. add(400)
3. add(300)
4. total()
5. add(300)
6. total()
Output:
700
800
Explanation:
The bulk discount takes 20 percent off a cart of 3 items or more.
At call 4 the cart holds 2 items, so it pays the full 700.
At call 6 it holds 3 items. 20 percent of 1000 is 200, so it pays 800.

Example 3:

Input:
Checkout::new()
1. set_discount(Box::new(AmountOff::new(500)))
2. add(300)
3. add(150)
4. total()
5. add(100)
6. total()
7. clear()
8. add(900)
9. total()
Output:
0
50
400
Explanation:
At call 4 the cart holds 450, less than the 500 off. The price never goes below 0.
At call 6 it holds 550, so it pays 50.
Call 7 empties the cart but keeps the discount.
So call 9 pays 900 - 500 = 400.

 

Constraints:

  • price is a whole number from 1 to 1,000.
  • percent is a whole number from 0 to 100.
  • amount is a whole number from 0 to 1,000,000.
  • min_items is a whole number from 1 to 100,000.
  • The cart's sum never goes over 20,000,000 cents.
  • At most 100,000 calls follow the constructor.

Note:

Each call should run in O(1) time, so keep the cart's sum and count, not its list of prices. The sum is at most 20,000,000. So total * percent is at most 2,000,000,000, which fits in a 32-bit integer.

STEP 1 OF 20
    Ctrl + Enter to check

    Strategy Pattern in Rust

    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free