建造者模式加策略模式计算个人工资

定义

建造这模式 又名生成器模式,是一种对象构建模式。它可以将复杂对象的建造过程抽象出来(抽象类别),使这个抽象过程的不同实现方法可以构造出不同表现(属性)的对象

建造者模式 是一步一步创建一个复杂的对象,它允许用户只通过指定复杂对象的类型和内容就可以构建它们,用户不需要知道内部的具体构建细节。

建造者模式优点

  • 客户端不必知道产品内部组成的细节,将产品本身与产品的创建过程解耦,使得相同的创建过程可以创建不同的产品对象。
  • 每一个具体建造者都相对独立,而与其他的具体建造者无关,因此可以很方便地替换具体建造者或增加新的具体建造者, 用户使用不同的具体建造者即可得到不同的产品对象 。
  • 可以更加精细地控制产品的创建过程 。将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰,也更方便使用程序来控制创建过程。
  • 增加新的具体建造者无须修改原有类库的代码,指挥者类针对抽象建造者类编程,系统扩展方便,符合 “开闭原则”

建造者模式的缺点

  • 增加新的具体建造者无须修改原有类库的代码,指挥者类针对抽象建造者类编程,系统扩展方便,符合 “开闭原则”
  • 产品内部变化很复杂的情况: 如果产品的内部变化复杂,可能会导致需要定义很多具体建造者类来实现这种变化,导致系统变得很庞大。
        // 我们要建造一个人的信息,包括基本信息,岗位信息,部门信息, 考勤信息。
        // 要求我们算出这个人的每月工资
        // 工资基本构成
        // 岗位等级 1=>1000,2=>2000,3=>3000
        // 出勤一天100
        // 请假一天扣100
        // 请算出小明,年龄=>20,性别=>男,住址=>河北省,岗位=>前端开发,等级=>2,
        // 部门=>网络部,出勤=>25天,请假=>5天,扣款=>300
        class People {
            // 基本信息
            basic({name, age, gender, address}) {
                this.name = name; // 姓名
                this.age = age;  // 年龄
                this.gender = gender; // 性别
                this.address = address; // 住址
            }
            // 岗位信息
            jobsInfo({jobs, level}) {
                this.jobs = jobs; // 岗位
                this.level = level; // 等级
            }
            // 部门信息
            departmentInfo({department}) {
                this.department = department; // 部门
            }
            // 考勤信息
            attendanceInfo({attendanceDays, leaveDays}) {
                this.attendanceDays = attendanceDays; // 出勤天数
                this.leaveDays = leaveDays // 请假天数
            }
            // 工资信息
            wageInfo({deductions}) {
                this.deductions = deductions; // 扣款
            }
        }
        let _basic = {
                name:'小明',
                age:'20',
                gender:'男',
                address:'河北省'
            },
            _jobsInfo = {
                jobs:'前端开发',
                level:2
            },
            _departmentInfo = {
                department:'网络部'
            },
            _attendanceInfo = {
                attendanceDays:25,
                leaveDays:5
            },
            _wageInfo = {
                deductions:300
            }
        class Builders extends People{
            constructor(strategy) {
                super();
                this.basic(_basic);
                this.jobsInfo(_jobsInfo);
                this.departmentInfo(_departmentInfo);
                this.attendanceInfo(_attendanceInfo);
                this.wageInfo(_wageInfo);
                this.wageMoney =  strategy;
            }
            init(){
                return this.wageMoney.realWages();
            }
        }
        class Strategy extends Builders{
            constructor({jobsMoney, attendancMoney, leaveMoney, deductionsMoney}){
                super();
                this.wage = 0;
                this._jobsMoney = jobsMoney;
                this._attendancMoney = attendancMoney;
                this._leaveMoney = leaveMoney;
                this._deductionsMoney = deductionsMoney;
            }
            realWages(){
                this.jobsWage(this._jobsMoney)
                this.attendancWage(this._attendancMoney,this._leaveMoney);
                this.deductionsWage(this._deductionsMoney);
                return `姓名:${this.name},年龄:${this.age},住址:${this.address},岗位:${this.jobs},等级:${this.level},部门:${this.department},出勤:${this.attendanceDays},请假:${this.leaveDays},扣款:${this.deductions},工资${this.wage}`;
            }
            jobsWage(jobsMoney){
                // 等级工资
                this.wage += this.level * jobsMoney;
                console.log(this.wage,'等级')
            }
            attendancWage(attendancMoney,leaveMoney){
                // 全勤工资
                this.wage += this.attendanceDays * attendancMoney;
                this.wage = this.wage - this.leaveDays * leaveMoney;
                console.log(this.wage,'出勤')
            }
            deductionsWage(deductionsMoney){

                this.wage = this.wage - deductionsMoney;
                console.log(this.wage,'扣款')
            }
        }
        let peopleInfo = new Builders(new Strategy({jobsMoney:1000,attendancMoney:100,leaveMoney:100,deductionsMoney:300}));
        console.log(peopleInfo.init()) // 姓名:小明,年龄:20,住址:河北省,岗位:前端开发,等级:2,部门:网络部,出勤:25,请假:5,扣款:300,工资3700

猜你喜欢

转载自www.cnblogs.com/mengxiangji/p/11070826.html