Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript 设计模式(建造者模式) #28

Open
wl05 opened this issue May 24, 2019 · 0 comments
Open

javascript 设计模式(建造者模式) #28

wl05 opened this issue May 24, 2019 · 0 comments

Comments

@wl05
Copy link
Owner

wl05 commented May 24, 2019

本系列文章来源于《Javascript设计模式》(张容铭版),做一个记录。

概念

简单的说如果创建的对象复杂,并且这个对象可以拆分成不同的部分那么我们可以将这个类的工作拆分到多个类中来完成。我们先创建三个类。

// 创建一个人
var Human = function (param) {
    this.skill = param && param.skill || '保密';
    this.hobby = param && param.hobby || '保密';
}

Human.prototype = {
    constructor: Human.constructor,// 原文中没有这一行
    getSkill: function () {
        return this.skill;
    },
    getHobby: function () {
        return this.hobby;
    }
}

// 实例化姓名类
var Named = function (name) {
    var that = this;
    // 解析姓名的姓和名
    (function (name, that) {
        that.wholeName = name;
        if (name.indexOf(' ') > -1) {
            that.FirstName = name.slice(0, name.indexOf(' '));
            that.secondName = name.slice(name.indexOf(' '));
        }
    })(name, that);
}
// 实例化职位类
var Work = function (work) {
    var that = this;
    (function (work, that) {
        switch (work) {
            case 'code':
                that.work = '工程师';
                that.wordDesc = '每天沉醉于编程';
                break;
            case 'UI':
            case 'UE':
                that.work = '设计师';
                that.wordDesc = '设计更似一种艺术';
                break;
            case 'teach':
                that.work = '教师';
                that.wordDesc = '分享也是一种快乐';
                break;
            default:
                that.work = work;
                that.wordDesc = '对不起,我们还不清楚您所选择职位的相关描述';
        }

    })(work, that)
}
// 更换期望职位
Work.prototype.changeWork = function (work) {
    this.work = work;
}
// 添加对职位的描述
Work.prototype.changeDesc = function (desc) {
    this.wordDesc = desc;
}

现在我们有了Human、Named、Work三个类。我们的最终目的是要创建一位应聘者,所以需要组合调用上面的三个类。现在我们来创建一个应聘者类,

var Person = function (name, work) {
    var _person = new Human();
    _person.name = new Named(name);
    _person.work = new Work(work);
    return _person;
}

我们在这个类中实例化了一个人,然后赋值给name,work。最后返回这个实例。现在我们来测试代码,看看是否达到目的。

var person = new Person('小 明', 'code');
console.log(person.skill); // 保密
console.log(person.name.FirstName); // 小
console.log(person.work.work); // 工程师
console.log(person.work.wordDesc); // 每天沉醉于编程
person.work.changeDesc('更改一下职位描述'); 
console.log(person.work.wordDesc); // 更改一下职位描述

这样我们就完成了一个创建者模式。

完整的代码:

// 创建一个人
var Human = function (param) {
    this.skill = param && param.skill || '保密';
    this.hobby = param && param.hobby || '保密';
}

Human.prototype = {
    constructor: Human.constructor,
    getSkill: function () {
        return this.skill;
    },
    getHobby: function () {
        return this.hobby;
    }
}

// 实例化姓名类
var Named = function (name) {
    var that = this;
    // 解析姓名的姓和名
    (function (name, that) {
        that.wholeName = name;
        if (name.indexOf(' ') > -1) {
            that.FirstName = name.slice(0, name.indexOf(' '));
            that.SecondName = name.slice(name.indexOf(' '));
        }
    })(name, that);
}
// 实例化职位类
var Work = function (work) {
    var that = this;
    (function (work, that) {
        switch (work) {
            case 'code':
                that.work = '工程师';
                that.wordDesc = '每天沉醉于编程';
                break;
            case 'UI':
            case 'UE':
                that.work = '设计师';
                that.wordDesc = '设计更似一种艺术';
                break;
            case 'teach':
                that.work = '教师';
                that.wordDesc = '分享也是一种快乐';
                break;
            default:
                that.work = work;
                that.wordDesc = '对不起,我们还不清楚您所选择职位的相关描述';
        }

    })(work, that)
}
// 更换期望职位
Work.prototype.changeWork = function (work) {
    this.work = work;
}
// 添加对职位的描述
Work.prototype.changeDesc = function (desc) {
    this.wordDesc = desc;
}


var Person = function (name, work) {
    var _person = new Human();
    _person.name = new Named(name);
    _person.work = new Work(work);
    return _person;
}

var person = new Person('小 明', 'code');
console.log(person.skill); // 保密
console.log(person.name.FirstName); // 小
console.log(person.work.work); // 工程师
console.log(person.work.wordDesc); // 每天沉醉于编程
person.work.changeDesc('更改一下职位描述');
console.log(person.work.wordDesc); // 更改一下职位描述
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant