设计模式笔记—开闭原则


     开闭原则

    概念:英文名称是Open Close Principle,缩写是OCP,,软件中的对象(类,模块,函数等)应该对于扩展是开放的,但是,对于修改是封闭的。意思是尽量减少对以前代码的修改。理由:在软件的生命周期内,因为变化,升级和维护等原因需要对软件原有代码进行修改时,可能会将错误引入原本已经经过测试的旧代码中,破坏原有 的系统。因此,当软件需要变化时,我们应该尽量通过扩展的方式来实现变化,而不是通过修改已有的代码来实现,或者说尽量少的修改原有代码,因为有时候并不能做 到百分百的不修改。举例说明:

    案例:书籍打折

    书籍接口          

public interface IBook {
	//书籍名称
	public String getName();
	//书籍售价
 	public int getPrice();
	//书籍作者
	public String getAuthor();
}
               小说类          

public class NovelBook implements IBook {
	//书籍名称
	private String name;
	//价格
	private int price;
	//作者
	private String author;
	//通过构造函数传递书籍数据
	public NovelBook(String name, int price, String author) {
		super();
		this.name = name;
		this.price = price;
		this.author = author;
	}
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return this.name;
	}
	@Override
	public int getPrice() {
		// TODO Auto-generated method stub
		return this.price;
	}
	@Override
	public String getAuthor() {
		// TODO Auto-generated method stub
		return this.author;
	}
}
      书店售书类          

public class BookStore {
private final static ArrayList<IBook> bookList=
    new ArrayList<IBook>();
   static {
	   bookList.add(new NovelBook("天龙八部",3200,"金庸"));
	   bookList.add(new NovelBook("悲惨世界",5600,"雨果"));
	   bookList.add(new NovelBook("巴黎圣母院",3500,"雨果"));
   }
   public static void main(String[] args) {
	NumberFormat formatter = NumberFormat.getCurrencyInstance();
	formatter.setMaximumFractionDigits(2);
	System.out.println("--------------书店卖书记录--------------------");
	for(IBook book:bookList){
		System.out.println("书籍名称:"+book.getName()
		+"\t书籍作者:"+book.getAuthor()+"\t书籍价格:"+formatter.format(
				book.getPrice()/100.0)+"元");
	}
}
}

        分析:由于业绩下滑,书店开始打折销售,所有40元以上打9折,其他的8折销售。对于已经投产的项目来说,如何应对这样一个需求。三种方法可以解决这个问题:

       1)修改接口

               在IBook上新增加一个方法getOffPrice(),这样做的后果是实现类NovelBook要修改,同时BookStore中的main方法也要修改,IBook作为接口应该是稳定且可靠的,不应该经常发生变化,负责接口作为契约的作用就失去了效能。

        2)修改实现类

                  修改NovelBook类中的方法,直接在getPrice()中实现打折处理,这是一种常用做法,缺陷:由于该方法已经实现了打折处理,因此采购人员看到的也是打折后的价格,可能会因信息不对称而实现决策失误的情况。

        3) 通过扩展实现变化

                  增加一个子类OffNovelBook,覆写getPrice方法,从而实现打折,这个办法较好,修改也少,风险也小。如下:        

public class OffNovelBook extends NovelBook{
	public OffNovelBook(String name, int price, String author) {
		super(name, price, author);
	}
	//覆写销售价格
	@Override
	public int getPrice() {
		//原价
		int selfPrice = super.getPrice();
		int offPrice=0;
		if(selfPrice>4000){
			offPrice=selfPrice*90/100;//打九折
		}else{
			offPrice=selfPrice*80/100;//打八折
		}
		return offPrice;
	}
}
public class BookStore {
private final static ArrayList<IBook> bookList=
    new ArrayList<IBook>();
   static {
	   bookList.add(new OffNovelBook("天龙八部",3200,"金庸"));
	   bookList.add(new OffNovelBook("悲惨世界",5600,"雨果"));
	   bookList.add(new OffNovelBook("巴黎圣母院",3500,"雨果"));
   }
   public static void main(String[] args) {
	NumberFormat formatter = NumberFormat.getCurrencyInstance();
	formatter.setMaximumFractionDigits(2);
	System.out.println("--------------书店卖书记录--------------------");
	for(IBook book:bookList){
		System.out.println("书籍名称:"+book.getName()
		+"\t书籍作者:"+book.getAuthor()+"\t书籍价格:"+formatter.format(
				book.getPrice()/100.0)+"元");
	}
}
}
              优点:1)提高了复用性。  2)提高了可维护性。   
     

     

            













                

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值