大话设计模式-装饰模式的应用——分配工资问题

/**
 * @author Leo
 * @ClassName: BaseBonus
 * @Description: 所有奖金的基础类
 */
public abstract class BaseBonus {

    public BaseBonus(String name, double money) {
        this.name = name;
        this.money = money;
    }

    public BaseBonus() {
    }

    //奖金名字
    protected String name;
    //奖金基础金额
    protected double money;

    //奖金计算方法
    public abstract double CalcMethod();
}

 

/**
 * @ClassName: PersonalBonus
 * @Author: Leo
 * @Description: 个人奖金 被装饰的类
 * @Date: 2019/3/26 18:43
 */
public class PersonalBonus extends BaseBonus {

    public PersonalBonus() {
        super();
    }

    public PersonalBonus(String name, double money) {
        super(name, money);
    }

    @Override
    public double CalcMethod() {
        return money;
    }
}
/**
 * @ClassName: LeaderBonus
 * @Author: Leo
 * @Description: 业务主管奖金 被装饰的类
 * @Date: 2019/3/26 18:47
 */
public class LeaderBonus extends BaseBonus {

    public LeaderBonus(String name, double money) {
        super(name, money);
    }

    public LeaderBonus() {
        super();
    }

    @Override
    public double CalcMethod() {
        return money;
    }
}
/**
 * @ClassName: CalcMethod_1
 * @Author: Leo
 * @Description: 奖金的计算方法_1 是装饰类之一
 * @Date: 2019/3/26 18:56
 */
public class CalcMethod_1 extends LeaderBonus {
    private BaseBonus baseBonus;

    public CalcMethod_1(BaseBonus baseBonus) {
        this.baseBonus = baseBonus;
        this.money = this.baseBonus.money;
        this.name = this.baseBonus.name;
    }

    @Override
    public double CalcMethod() {
        //两倍工资
        this.baseBonus.money = 2 * baseBonus.CalcMethod();
        this.money = this.baseBonus.money;
        this.name = this.baseBonus.name;
        return this.baseBonus.money;
    }

    @Override
    public String toString() {
        return "CalcMethod_1{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
/**
 * @ClassName: CalcMethod_2
 * @Author: Leo
 * @Description: 奖金的计算方法_2 是装饰类之一
 * @Date: 2019/3/26 18:56
 */
public class CalcMethod_2 extends BaseBonus {
    private BaseBonus baseBonus;

    public CalcMethod_2(BaseBonus baseBonus) {
        this.baseBonus = baseBonus;
        this.money = baseBonus.money;
        this.name = baseBonus.name;
    }

    @Override
    public double CalcMethod() {
        //三倍工资
        this.baseBonus.money = 3 * baseBonus.CalcMethod();
        this.money = this.baseBonus.money;
        this.name = this.baseBonus.name;
        return this.baseBonus.money;
    }

    @Override
    public String toString() {
        return "CalcMethod_2{" +
                "name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}
/**
 * @ClassName: Main
 * @Author: Leo
 * @Description:
 * @Date: 2019/3/26 19:08
 */
public class Main {
    public static void main(String[] args) {
        CalcMethod_2 calcMethod_2 =
                new CalcMethod_2(
                        new CalcMethod_1(
                                new PersonalBonus("个人工资", 100)));
        calcMethod_2.CalcMethod();
        System.out.println(calcMethod_2.toString());
    }
}

 

全部评论

相关推荐

10-24 13:36
门头沟学院 Java
Zzzzoooo:更新:今天下午有hr联系我去不去客户端,拒了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务