做出来的软件具有很强的扩展性,扩展并不是修改以前的源代码,而是在以前的代码之外添加新的功能.
重点是对扩展开放,对修改关闭.
银行业务员下有很多子类:负责存款的银行业务员,负责取款的银行业务员,负责转账的银行业务员.
Client是进行模块的调用,不算模块的修改.除了客户端MainClass之外,其他都是模块:BankWorker、DrawingBankWorker、JiJinBankWorker、SavingBankWorker、zhuanzhangBankWorker
仅仅是通过添加子类就实现了功能的扩展.
特别是最重要的抽象模块不能再修改,就是这里的BankWorker模块没有再修改.接口是一定不要修改的,否则它的所有的实现子类都要修改.如果接口中的方法被修改了,Client中的调用接口的方法都会受到影响.所以最重要是抽象模块不能被修改.
/* * 银行业务员 * */public class BankWorker { //负责存款业务 public void saving() { System.out.println("进行存款操作"); } //负责取款业务 public void drawing(){ System.out.println("进行取款操作"); } //负责转账业务 public void zhuanzhang(){ System.out.println("进行转账操作"); } //负责基金的申购 public void jijin(){ System.out.println("进行基金申购操作"); }}
public class MainClass { public static void main(String[] args) { BankWorker bankWorker = new BankWorker(); //存款 bankWorker.saving(); //取款 bankWorker.drawing(); //转账业务 bankWorker.zhuanzhang(); //负责基金采购 bankWorker.jijin(); }}
package com.ibeifeng.ex2;/* * 银行业务员接口,是所有业务员的抽象父类 * */public interface BankWorker { public void operation();}
package com.ibeifeng.ex2;/* * 负责取款业务的业务员 * * */public class DrawingBankWorker implements BankWorker{ @Override public void operation() { // TODO Auto-generated method stub System.out.println("进行取款操作"); }}
package com.ibeifeng.ex2;public class JiJinBankWorker implements BankWorker { @Override public void operation() { // TODO Auto-generated method stub System.out.println("进行基金申购操作"); }}
package com.ibeifeng.ex2;public class MainClass { public static void main(String[] args) { BankWorker bankWorker = new SavingBankWorker(); bankWorker.operation(); BankWorker bankWorker2 = new DrawingBankWorker(); bankWorker2.operation(); BankWorker bankWorker3 = new zhuanzhangBankWorker(); bankWorker3.operation(); BankWorker bankWorker4 = new JiJinBankWorker(); bankWorker4.operation(); }}
package com.ibeifeng.ex2;/* * 负责存款的业务员 * */public class SavingBankWorker implements BankWorker { @Override public void operation() { // TODO Auto-generated method stub System.out.println("进行存款操作"); } }
package com.ibeifeng.ex2;/* * 负责转账业务的银行业务员 * */public class zhuanzhangBankWorker implements BankWorker{ @Override public void operation() { // TODO Auto-generated method stub System.out.println("进行转账操作"); }}