Java 6大设计原则(六)---开闭原则。

x33g5p2x  于2021-03-13 发布在 Java  
字(3.1k)|赞(0)|评价(0)|浏览(654)

英文名称: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;
	}
}

相关文章