Bonnes pratiques de programmation
Révision du cours
- DRY : Do not Repeat Yourself
- DOT : Do One Thing
- KISS : Keep It Simple, Stupid
- SRP : Single Responsibility Principle
Public, private, final
bodyColor
: publique et immutablecurrentSpeed
: privée et mutablegasolineLevel
: privée et mutablelicensePlate
: publique et immutableturnSignalState
: privée et mutable (avec l’ajout d’un accesseur publique)
Flacons
public class Bottle {
public final double capacity;
private double totalVolume;
private double concentration;
public final String label;
public Bottle(double capacity,
double totalVolume,
double concentration,
String label) {
this.capacity = capacity;
this.totalVolume = totalVolume;
this.concentration = concentration;
this.label = label;
}
private double getSyrupVolume(){
return concentration * totalVolume;
}
void pourLiquid(double volume, double concentration){
double newSyrupVolume = getSyrupVolume()
+ concentration*volume;
this.totalVolume += volume;
this.concentration = newSyrupVolume/totalVolume;
}
public void pourSyrup(double volume){
pourLiquid(volume,1);
}
public void pourWater(double volume){
pourLiquid(volume, 0);
}
public void transvase(Bottle pouringBottle, double volume){
volume = pouringBottle.drink(volume);
this.pourLiquid(volume, pouringBottle.concentration);
}
public double drink(double volume){
volume = Math.min(volume, this.totalVolume);
this.totalVolume -= volume;
return volume;
}
public String toString(){
return "Étiquette : " + this.label + " Volume : "
+ this.totalVolume + " Concentration "+
this.concentration;
}
}