专业编程教程与实战项目分享平台

网站首页 > 技术文章 正文

前端基础面试:建造者模式,单例模式,代理模式

ins518 2024-10-03 00:14:17 技术文章 15 ℃ 0 评论

建造者模式

Bash
'use strict';
var Human = function(param) {
 // 技能
 this.skill = param && param.skill || '保密';
 // 兴趣爱好
 this.hobby = param && param.hobby || '保密';
};
Human.prototype = {
 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.workDescript = '每天沉醉于编程';
 break;
 case 'UI':
 case 'UE':
 that.work = '设计师';
 that.workDescript = '设计更似一种艺术';
 break;
 case 'teach':
 that.work = '教师';
 that.workDescript = '分享也是一种快乐';
 break;
 default:
 that.work = work;
 that.workDescript = '对不起,我们还不清楚您所选择职位的相关描述';
 }
 })(work, that);
};
// 更换期望的职位
Work.prototype.changeWork = function(work) {
 this.work = work;
}
// 添加对职位的描述
Work.prototype.changeDescript = function(des) {
 this.workDescript = des;
};
/**
 * 应聘者建造者
 *
 * @param name 姓名(全名)
 * @param work 期望职位
 */
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('xiao ming', 'code');
console.log(person)

单例模式

单例模式(创建命名空间)

Bash
'use strict';
var A = {
 Util: {
 util_method1: function() {},
 util_method2: function() {}
 },
 Tool: {
 tool_method1: function() {},
 tool_method2: function() {}
 },
 Ajax: {
 get: function() {},
 post: function() {}
 },
 others: {
 // ...
 }
};
A.Util.util_method2();
A.Tool.tool_method1();
A.Ajax.get();

静态变量(无法修改的静态变量)

Bash
'use strict';
var Config = (function() {
 // 私有变量
 var config = {
 MAX_NUM: 100,
 MIN_NUM: 1,
 COUNT: 1000
 };
 // 返回取值器对象
 return {
 // 取值器方法
 get: function(name) {
 return config[name] ? config[name] : null;
 }
 }
})();
var count = Config.get('COUNT');
console.log(count);

惰性单例

Bash
'use strict';
// 惰性载入单例
var LazySingle = (function() {
 // 单例实例引用
 var _instance = null;
 // 单例
 function Single() {
 // 这里定义私有属性和方法
 return {
 publicMethod: function() {},
 publicProperty: '1.0'
 }
 }
 return function() {
 if (!_instance) {
 _instance = Single();
 }
 // 返回单例
 return _instance;
 }
})();
console.log( LazySingle().publicProperty ); // 1.0
console.log( LazySingle() === LazySingle() ); // true

代理模式

Bash
// 先声明美女对象
var girl = function (name) {
 this.name = name;
};
// 这是dudu
var dudu = function (girl) {
 this.girl = girl;
 this.sendGift = function (gift) {
 alert("Hi " + girl.name + ", dudu送你一个礼物:" + gift);
 }
};
// 大叔是代理
var proxyTom = function (girl) {
 this.girl = girl;
 this.sendGift = function (gift) {
 (new dudu(girl)).sendGift(gift); // 替dudu送花咯
 }
};


var proxy = new proxyTom(new girl("酸奶小妹"));
proxy.sendGift("999朵玫瑰");

Bash
欢迎关注

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表