????????????????????????????????????????????????????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;     
           }     
    }