???????????????
???????????? ???????[ 2013/5/27 13:39:02 ] ????????
????????????????????????????????????????????????????Part ?????????
?????????·????????PricePolicy???????????м?????????????????????????????????????????????????????????????????????? ???????????????????????????????????У???“?仯”?????PricePolicy?????檔???????y???????????????????Part???????????????????趨??????????????????????????ConcretePart????????????????л??????????????????“?仯”?????????????????????
public double totalPrice(Part[] parts) {
double total = 0.0;
for(int i = 0;i
total += parts[i].getPrice();
}
return total;
}
?????????????????????????????м????????????????????Part?????????????????????????????????????????????????????????????????????佫????OCP
???????????????????????????????????????????????????????е???????????????????????OCP??
public double totalPrice(Part[] parts) {
double total = 0.0;
for(int i = 0;i
if(parts[i] instanceof Motherboard)
total += (1.45*parts[i].getPrice());
else if(parts[i] instanceof Memory)
total += (1.27*parts[i].getPrice());
else
total += parts[i].getPrice();
}
return total;
}
????β???????μ???????????????ò?????totalPrice()??????????“??????????”??????????????????ζ????????ò?????Щ???????????????????OCP????????????????????
???????????????汾??totalPrice()????????????????????Part??getPrice()?????С?
??????Part??ConcretePart????????
Java???? ??????
public class Part {
private double basePrice;
public void setPrice(double price) {
basePrice = price;
}
public double getPrice() {
return basePrice;
}
}
public class Motherboard extends Part {
public double getPrice() {
return 1.45*basePrice;
}
}
public class Memory extends Part {
public double getPrice() {
return 1.27*basePrice;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11