原:
package com.maom.design.P_1_Open_Closed_Principle;
/**
* @Title: P_1_Open_Closed_Principle_0
* @Description: 1. 开放封闭原则
* @Author maohb
* @Version 1.0
*/
public class P_1_Open_Closed_Principle_0 {
public static void main(String[] args) {
BankWorker0 bankWorker = new BankWorker0();
//存款
bankWorker.saving();
//取款
bankWorker.drawing();
//基金
bankWorker.jijin();
}
}
/** 繁忙的银行业务员 */
class BankWorker0 {
// 负责存款业务
public void saving() {
System.out.println("进行存款操作");
}
// 负责取款业务
public void drawing() {
System.out.println("进行取款操作");
}
// 负责基金的申购
public void jijin() {
System.out.println("进行基金申购操作");
}
}
改:
package com.maom.design.P_1_Open_Closed_Principle;
/**
* @Title: P_1_Open_Closed_Principle
* @Description: 1. 开放封闭原则
* @Author maohb
* @Version 1.0
*/
public class P_1_Open_Closed_Principle {
public static void main(String[] args) {
BankWorker bankWorker = new SavingBankWorker();
bankWorker.operation();
BankWorker bankWorker2 = new DrawingBankWorker();
bankWorker2.operation();
BankWorker bankWorker3 = new JiJinBankWorker();
bankWorker3.operation();
}
}
/** 轻松的银行业务员接口,是所有银行业务员的抽象父类。 */
interface BankWorker {
public void operation();
}
/** 负责存款业务的业务员 */
class SavingBankWorker implements BankWorker {
public void operation() {
System.out.println("进行存款操作");
}
}
/** 负责取款业务的业务员 */
class DrawingBankWorker implements BankWorker{
public void operation() {
System.out.println("进行取款操作");
}
}
/** 负责基金申购业务的业务员 */
class JiJinBankWorker implements BankWorker {
public void operation() {
System.out.println("进行基金申购操作");
}
}