js-设计模式

构造器模式

/** es5 */
//这样赋值对象 重复性工作很多
var whh = {};
whh.name = '王华华';
whh.age = 18;
whh.sex = '男';
whh.scope = 98;

var lqd = {};
lqd.name = '李全蛋';
lqd.age = 20;
lqd.sex = '男';
lqd.scope = 46;

console.log(whh.name)
console.log(lqd.name)

//构造器模式避免重复性操作直接new对象
function Student(name, age, sex, scope) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.scope = scope;
    // 弊端,每个对象都会创建一个这个方法,而这个方法是一样的,每次都创建会浪费资源
    this.sumscope = function () {
        return this.scope + 20
    }
}


var zsf = new Student('张三丰', 20, '男', 100);
console.log(zsf.name)
console.log(zsf.sumscope())

/** es6 */

class Student1 {
    constructor(name, age, sex, scope) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.scope = scope;
    }

    sumscope() {
        return this.scope + 20
    }
}
var ls = new Student1('李四', 19, '男', 200);
console.log(ls.name)
console.log(ls.sumscope()) 

原型模式


//构造器模式避免重复性操作直接new对象
function Student(name, age, sex, scope) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.scope = scope; 
}
// 所有对象共享这个方法
Student.prototype.sumscope = function(){
    return this.scope + 20
}

var zsf = new Student('张三丰', 20, '男', 100);
console.log(zsf.name)
console.log(zsf.sumscope())

构建者模式 1

/** es5 */
// 就是得加getXXX setXXX 好处是方便控制这些值,比如年龄不能大于120
function Student(name, age, sex, scope) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    this.scope = scope; 
}
// 构建者
function StudentBuilder(){
    this.Student = new Student(); 
}  

StudentBuilder.prototype.setName = function(name){
    if(name == '张三') throw '你不能叫张三';
    this.Student.name = name; 
}

StudentBuilder.prototype.setAge = function(age){
    if(age > 60) throw '年龄大于60';
    this.Student.age = age;
}

StudentBuilder.prototype.setSex = function(sex){
    if(sex != '男' && sex != '女') throw '性别有误';
    this.Student.sex = sex;
}

StudentBuilder.prototype.setScope = function(scope){
    if(scope > 120) throw '分数有误';
    this.Student.scope = scope;
}

StudentBuilder.prototype.build = function(){
    return this.Student;
}
var zsf = new StudentBuilder();
zsf.setName('张三丰');
zsf.setAge(18);
zsf.setScope(100);
zsf.setSex('男');
console.log(zsf.build()) 

构建者模式2

/** es6 */
// 就是得加getXXX setXXX 好处是方便控制这些值,比如年龄不能大于120
class Student{
     
}
// 构建者
class StudentBuilder {
    constructor(){
        this.Student = new Student();
    }

    setName(name){
        if(name == '张三') throw '你不能叫张三';
        this.Student.name = name; 
    }

     setAge(age){
        if(age > 60) throw '年龄大于60';
        this.Student.age = age;
    }

     setSex(sex){
        if(sex != '男' && sex != '女') throw '性别有误';
        this.Student.sex = sex;
    }

     setScope(scope){
        if(scope > 120) throw '分数有误';
        this.Student.scope = scope;
    }

     build(){
        return this.Student;
    }
}

const zsf = new StudentBuilder();
zsf.setName('张三丰');
zsf.setAge(18);
zsf.setScope(100);
zsf.setSex('男');
console.log(zsf.build()) 

抽象工厂模式

function Student(){
    this.info = '学生11111';
}


function Teacher(){
    this.info = '老师1111';
}

function StudentFactory(){
    return new Student();
}

function TeacherFactory(){
    return new Teacher();
}


function userProducer(factory){
    switch (factory) {
        case 'student':
                return StudentFactory;
        case 'teacher':
                return TeacherFactory; 
        default:
            throw '没有这个角色';
            
    }
}

var factory = userProducer('student');
var t = new factory();
console.log(t)

单例模式1

// 方式1  全局变量 个人理解  
// 例如
var a = { baobao: 1 }

// 全局变量操作的时候 用对象的方式管理全局变量
// 方式2  
function GirlFriend() {
    if (GirlFriend.fn) {
        return GirlFriend.fn;
    } else {
        GirlFriend.fn = this;
        GirlFriend.fn.gift = {
            baobao: 1
        };
    }
}
// 这样女朋友始终是一个了,不会随着new对象包包被重置为1
var gf = new GirlFriend();
gf.gift.baobao = 2;
console.log(gf.gift)

var gf1 = new GirlFriend();
console.log(gf1.gift)

var gf2 = new GirlFriend();

console.log(gf2.gift)

单例模式2 node

var singleton = null;

function Singleton (){
    this.baobao = 1;
}

module.exports = {
    getInstance(){
        if(!singleton){
            singleton = new Singleton();
        }
        return singleton
    }
}
//   方式3 感觉这个方式比较符合单例模式的定义
// 懒汉模式
const Singleton = require('./7.单例模式');
let s1 = Singleton.getInstance();
s1.baobao = 2;
console.log(s1) // 2
let s2 = Singleton.getInstance();
console.log(s2) // 2
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值