## 英文名稱:Open Closed Principle,OCP
**定義**:一個軟件實體如類、模塊和函數應該對擴展開發,對修改關閉。
**大意**:一個軟件實體應該通過擴展來實現變化,而不是通過修改已有的代碼來實現變化。
開閉原則就是java世界里最基礎的設計原則。
軟件實體包括:
* 項目或軟件產品中按照一定的邏輯規則劃分的模塊
* 抽象和類
* 方法
**一個實例**:
* IBook定義了數據的三個屬性:名稱,作者,價格
* BookStore是書店
* NovelBook是一個具體的實現類

~~~
public class BookStore {
public static List<NovelBooks> listBooks = new ArrayList<NovelBooks>();
static{
listBooks.add(new NovelBooks("三重門","韓寒",3000));
listBooks.add(new NovelBooks("人生","路遙",2400));
listBooks.add(new NovelBooks("荊棘鳥","考琳·麥卡洛",5000));
listBooks.add(new NovelBooks("從你的全世界路過","張嘉佳",2500));
}
public static void main(String[] args) {
//返回默認的貨幣格式
NumberFormat formatter = NumberFormat.getCurrencyInstance();
for(NovelBooks book: listBooks){
System.out.println("書籍名稱:"+book.getName()+"\t書籍作者:"+book.getAuthor()+"\t書籍價格:"+
formatter.format(book.getPrice()/100.0)+"元");
}
}
}
interface IBook{
public String getName();
public String getAuthor();
public int getPrice();
}
class NovelBooks implements IBook{
private int price;
private String name;
private String author;
public NovelBooks(String name,String author,int price) {
this.author = author;
this.price =price;
this.name = name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
@Override
public String getAuthor() {
// TODO Auto-generated method stub
return this.author;
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return this.price;
}
}
~~~
**需求更改**:
當書店推出打折暢銷,來獲取更多的購買量。書店規定40元以下的書籍8折出售,50元以上的數據9折出售。
那么如果在NovelBooks類中修改getPrice方法就會導致代碼修改量變大。
解決方案:
通過擴展實現變化。
遵守開閉原則,對軟件實體進行擴展來達到改變需求的目的,而不是修改已經存在的代碼。
定義一個打折類offNovelBooks繼承自NovelBooks,覆寫getPrice方法

~~~
public class BookStore {
public static List<NovelBooks> listBooks = new ArrayList<NovelBooks>();
static{
listBooks.add(new OffNovelBooks("三重門","韓寒",3000));
listBooks.add(new OffNovelBooks("人生","路遙",2400));
listBooks.add(new OffNovelBooks("荊棘鳥","考琳·麥卡洛",5000));
listBooks.add(new OffNovelBooks("從你的全世界路過","張嘉佳",2500));
}
public static void main(String[] args) {
//返回默認的貨幣格式
NumberFormat formatter = NumberFormat.getCurrencyInstance();
for(NovelBooks book: listBooks){
System.out.println("書籍名稱:"+book.getName()+"\t書籍作者:"+book.getAuthor()+"\t書籍價格:"+
formatter.format(book.getPrice()/100.0)+"元");
}
}
}
interface IBook{
public String getName();
public String getAuthor();
public int getPrice();
}
class NovelBooks implements IBook{
private int price;
private String name;
private String author;
public NovelBooks(String name,String author,int price) {
this.author = author;
this.price =price;
this.name = name;
}
@Override
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
@Override
public String getAuthor() {
// TODO Auto-generated method stub
return this.author;
}
@Override
public int getPrice() {
// TODO Auto-generated method stub
return this.price;
}
}
class OffNovelBooks extends NovelBooks{
public OffNovelBooks(String name, String author, int price) {
super(name, author, price);
// TODO Auto-generated constructor stub
}
@Override
public int getPrice() {
int rePrice = super.getPrice();
int nowPrice = 0;
if(rePrice>4000){
nowPrice = rePrice*80/100;
}else{
nowPrice = rePrice*90/100;
}
return nowPrice;
}
}
~~~
- 前言
- 6大設計原則(一)---單一職責原則
- 6大設計原則(二)---里氏替換原則
- 6大設計原則(三)---依賴倒置原則
- 6大設計模式(四)----接口隔離原則
- 6大設計原則(五)---迪米特法則
- 6大設計原則(六)---開閉原則。
- 設計模式(一)---單例模式
- 設計模式(二)---工廠方法模式
- 設計模式(三)---抽象工廠模式
- 設計模式(四)---模板方法模式
- 設計模式(五)---建造者模式
- 設計模式(六)---代理模式
- 設計模式(七)---原型模式
- 設計模式(八)---中介者模式
- 設計模式(九)---命令模式
- 設計模式(十)---責任鏈模式
- 設計模式(十一)---裝飾模式
- 設計模式(十二)---策略模式
- 設計模式(十三)---適配器模式
- 設計模式(十四)---迭代器模式
- 設計模式(十五)---組合模式
- 設計模式(十六)---觀察者模式
- 設計模式(十七)---門面模式
- 設計模式(十八)---備忘錄模式
- 設計模式(十八)---訪問者模式
- 設計模式(二十)---狀態模式
- 設計模式(二十二)---享元模式
- 設計模式(二十三)---橋梁模式