面向对象编程写的面向过程代码

在面向对象语言中,我们很容易写出面向过程代码

1.比如get,set方法,很多时候,我们直接用IDE生成这部分代码,这是十分忌讳的,与面向对象的封装相违背,有的属性我们时不能给set权限的,还有list容器等不能给get权限,以免用到这个类的程序员私自修改容器对象造成bug

public class ShoppingCart {
  private int itemsCount;
  private double totalPrice;
  private List<ShoppingCartItem> items = new ArrayList<>();
<p>public int getItemsCount() {
return this.itemsCount;
}</p>
<p>public void setItemsCount(int itemsCount) {
this.itemsCount = itemsCount;
}</p>
<p>public double getTotalPrice() {
return this.totalPrice;
}</p>
<p>public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}</p>
<p>public List<ShoppingCartItem> getItems() {
return this.items;
}</p>
<p>public void addItem(ShoppingCartItem item) {
items.add(item);
itemsCount++;
totalPrice += item.getPrice();
}
// ...省略其他方法...
}

2.全局常量写在一个类里,这样不太好,还有Utils里面的静态方法,有时我们确实得用到面向过程编程,因为面向过程更符合我们得思维,但是我们得尽量保证类单一职责,把Constants类,Uilts不断细化

3. mvc的贫血模型也是面向过程的,这个后面写