古代印刷術就是把文章內容刻在石頭或者木板上,每次修改內容,咱們須要從新刻畫,原來的那塊也就廢棄了,這種方式很浪費人力物力。後來慢慢出現了活字印刷術,人們只須要預先把全部的文字刻畫好,而後拼在一塊兒就行成文章,每一個字都不浪費,大大節省了物力人工。php
直接用if-else這種寫死的方法編寫工資計算模塊,每次修改必須修改原先運行穩定的代碼,耦合性高,風險大。(印刷術)java
在抽象父類中定義好功能,利用子類實現具體各個工種的薪資計算,鬆耦合,可拓展。(活字印刷術)bash
package com.design.design.simpleFactory;
/**
* @author zhaozha
* @date 2019/11/3 上午9:32
*/
public class Constant {
public static final int JAVA_SALARY_INCENTIVE = 10;
public static final int PHP_SALARY_INCENTIVE = 1;
}
複製代碼
package com.design.design.simpleFactory;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import java.math.BigDecimal;
/**
* @author zhaozha
* @date 2019/11/3 上午9:17
*/
@Data
@Builder
@AllArgsConstructor
@RequiredArgsConstructor
public class Salary {
private int day;
private BigDecimal datPay;
}
複製代碼
package com.design.design.simpleFactory;
import java.math.BigDecimal;
/**
* @author zhaozha
* @date 2019/11/3 上午9:12
*/
public abstract class BaseSalaryCalculation {
/**
* 計算月薪
* @param salary 計費對象
*/
public abstract BigDecimal getSalary(Salary salary);
}
複製代碼
package com.design.design.simpleFactory;
import java.math.BigDecimal;
import static com.design.design.simpleFactory.Constant.JAVA_SALARY_INCENTIVE;
/**
* @author zhaozha
* @date 2019/11/3 上午9:14
*/
public class JavaSalaryCalculation extends BaseSalaryCalculation {
/**
* JAVA激勵10天
*
* @param salary 計費對象
* @return 月薪
*/
@Override
public BigDecimal getSalary(Salary salary) {
return salary.getDatPay().multiply(new BigDecimal(salary.getDay() + JAVA_SALARY_INCENTIVE));
}
}
複製代碼
package com.design.design.simpleFactory;
import java.math.BigDecimal;
import static com.design.design.simpleFactory.Constant.PHP_SALARY_INCENTIVE;
/**
* @author zhaozha
* @date 2019/11/3 上午9:15
*/
public class PHPSalaryCalculation extends BaseSalaryCalculation {
/**
* PHP激勵1天
*
* @param salary 計費對象
* @return 月薪
*/
@Override
public BigDecimal getSalary(Salary salary) {
return salary.getDatPay().multiply(new BigDecimal(salary.getDay() + PHP_SALARY_INCENTIVE));
}
}
複製代碼
package com.design.design.simpleFactory;
/**
* @author zhaozha
* @date 2019/11/3 上午9:22
*/
public class SalarySimpleFactory {
public static BaseSalaryCalculation getSalaryCalculation(Class c) {
BaseSalaryCalculation baseSalaryCalculation = null;
try {
baseSalaryCalculation = (BaseSalaryCalculation) Class.forName(c.getName()).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return baseSalaryCalculation;
}
}
複製代碼
package com.design.design.simpleFactory;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
/**
* @author zhaozha
* @date 2019/11/3 上午9:33
*/
@Slf4j
public class Test {
public static void main(String agrs[]) {
BaseSalaryCalculation javaSalaryCalculation =
SalarySimpleFactory.getSalaryCalculation(JavaSalaryCalculation.class);
final Salary salary = Salary.builder().day(20).datPay(new BigDecimal("300.20")).build();
log.info("JAVA月薪:{}", javaSalaryCalculation.getSalary(salary));
BaseSalaryCalculation phpSalaryCalculation =
SalarySimpleFactory.getSalaryCalculation(PHPSalaryCalculation.class);
log.info("PHP月薪:{}", phpSalaryCalculation.getSalary(salary));
}
}
複製代碼