简体   繁体   中英

Which patterns to use - how to design

All,

I am the process of applying design thinking to everyday problems. on the process, i need some help on the following. Assume a car renting company such as hertz is needing an application, along with regular data, they need the ability to offer customers offers based on certain criteria.

Criteria are

  1. All listed holidays (not weekends) - get 10% off on hire price
  2. Labor day alone get 15% off on hire price
  3. if taking > 3 days for listed holidays - 20 % off
  4. if taking >3 days on labor day - 25% off
  5. etc - more will come

Now I can think of decorators to start off such as

IVehicleBase
{  
 Props: Make, Model, HirePrice, HireTimePeriodinDays   
}

Concrete classes:

public class FordExplorer : VehicleBase   
{
    public override string Make
    {
        get { return "Ford"; }
    }

    public override string Model
    {
        get { return "Explorer"; }
    }

    public override double HirePrice
    {
        get { return 450; }
    }

    public override int HireTimePeriodinDays
    {
        get { return 1; }
    }
}

Similarly for many other classes (cars).

now how do i decorate the special offers ?

I Can think of a vehicleDecoratorBase, but if the offer is specific to just the holidays then fine (make two concrete implementations for labor / other listed holidays), but we have holidays combined with hireDate which is a domain property of car.

I hope I am making sense here. Any advices pls ?

Cheers

I would say the rentals, prices, special offers etc. do not belong to the vehicle: these are separate abstraction(s) . Especially so as the kinds of offers you list above do not seem to be linked to a specific vehicle model, only to calendar days / length of rental etc.

So don't try to add these as decorators to the vehicle. Instead, create a separate class (hierarchy) to deal with them.

Note that based on your description you have two use cases:

  • the rental agent needs to be able to enumerate all existing offers to the customer like "if you start your rental next Monday, you will get a 10% discount due to this offer, alternatively if you start on Tuesday but extend the rental to 3 days you will get 20% off" etc.
  • the (estimated) price of a concrete rental needs to be calculated taking into account all applicable discounts.

So on the one hand you should store an instance of each valid offer in a collection , which allows the agent to list them one by one, along with a textual description. (Note that as the offers are most likely stateless, you can have a single shared instance of each.)

OTOH you need functionality for each offer to decide whether it is applicable for a given concrete rental. This can be implemented within the individual offer classes, then invoked polymorphically by eg an offer selector (a special kind of factory), which iterates over the collection of offers and selects the ones for the given rentals (nice, you can immediately reuse the collection of offers for a different purpose!). The applicable offers then in fact operate on the collection of rental charges (ie not necessarily directly on the rental object itself), modifying / removing charges as applicable.

As a side note, if you represent each vehicle model with a separate derived class of IVehicleBase , you would have an awful lot of derived classes in real life, and moreover you would need to add new ones regularly as new models come out and get incorporated into the fleet. And requiring a code change whenever a new vehicle model appears is a maintenance headache in the long run. Of course, if this is just an experimental / toy project, maintenance may not be an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM