Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

What seems most natural to me is that the gas station would have a fillUp(IFillableWithGas) method that takes an object with the IFillableWithGas interface, which has a method that returns the amount it was able to take and a method to put that much in. The gas station is the one "doing" the action "to" things that are able to take gas, and it's the gas station's responsibility to know how to pump up the gas from the tank and calculate the price. gasStation->fillUp could look like (leaving it at a gas station with only one pump for simplicity)

    float price = 0;
    while (!vehicle.gasIsFull()){
        Gas gas = this.undergroundGasTank.getGas();
        vehicle.depositGas(gas);
        price += gas.getAmount() * this.gasPricePerGallon;
        this.display(price);
    }
It would be out of place for a car or gas canister class to know how to use the undergroundGasTank object and to calculate the price themselves.


LOL. Who said anything about an underground gas tank, pricing, or a display? If you're adding pricing, you have to add a payment system. It'd be out of place for a Vehicle to know anything about paying a bill. Objects just model reality; they don't have to mirror reality.


It's just a demonstration of how the hierarchy works in the abstract. And it's just displaying the price on the meter, not saying the "car" has to deal with that. The driver would, or the player, if you look at this like game code, which would make sense.


Dude, it was just an example for a style discussion. You're missing the point of the discussion and way over-engineering the example.


I'm just saying I don't think the "There is no right answer" statement works here because there seems to be one right answer to me.


It's weird how inexperienced people are the most rigid and dogmatic. Experienced people know that there are always multiple ways of doing everything.


If you have to resort to implying lack of experience is the only reason someone wouldn't like your example, that doesn't reflect well.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: