mercredi 30 juin 2021

Apply a design pattern to solve this problem and not use if/else

I am trying to improve my skills with design patterns, so please be easy on me :) In my university I often have to deal with problems where the algorithm is defined, but data structures it is working on are different.

I have the following problem:

  • If the client is under 18, reduce the base price by 10%.
  • If the city is ‘London’ the price of the product is 1$.
  • If the client has a membership type of ‘gold’ the product is free (£0).

so I tried this:

    if($customer <18) {
    $price = $price-10;
} 

if($city == "London") {
    $price = 1;
}

if($client->membership == "gold") {
    $price = 0;
}

Can someone help me to make it in a nice pattern, like Factory or Strategy? Are they the right one? If not could you tell me which one and give me an example? Many thanks in advance.

Aucun commentaire:

Enregistrer un commentaire