《javascript设计模式》笔记之第五章:单体模式
一:单体的基本结构:
var Singleton = {
attribute1: true,
attribute2: , method1: function() { },
method2: function(arg) { }
};
var GiantCorp = {}; GiantCorp.Common = {
// A singleton with common methods used by all objects and modules.
}; GiantCorp.ErrorCodes = {
// An object literal used to store data.
}; GiantCorp.PageHandler = {
// A singleton with page specific methods and attributes.
};
Namespace.PageName = { // Page constants.
CONSTANT_1: true,
CONSTANT_2: , // Page methods.
method1: function() { },
method2: function() { }, // Initialization method.
init: function() { }
}
addLoadEvent(Namespace.PageName.init);
var formID = document.getElementById("form");
formID.addEventListener('submit',formSubmit,false); function formSubmit(e) {
//阻止表单默认动作,然后用ajax发送数据
//接受数据之后调用callBack函数
} function callBack(data) {
//用ajax提交表单之后的回调函数
var GiantCorp = window.GiantCorp || {};
GiantCorp.RegPage = {
formID: 'form',
callBack: function(data) {
//用ajax提交表单之后的回调函数
},
formSubmit: function(e) {
//阻止表单默认动作,然后用ajax发送数据
//接受数据之后调用callBack函数
},
init: function(e) {
GiantCorp.RegPage.formEl = document.getElementById(GiantCorp.RegPage.formID);
GiantCorp.RegPage.formEl.addEventListener('submit',GiantCorp.RegPage.formSubmit,false);
}
} GiantCorp.RegPage.init();
GiantCorp.DataParser = {
// Private methods.
_stripWhitespace: function(str) {
return str.replace(/\s+/, '');
},
_stringSplit: function(str, delimiter) {
return str.split(delimiter);
}, // Public method.
stringToArray: function(str, delimiter, stripWS) {
if(stripWS) {
str = this._stripWhitespace(str);
}
var outputArray = this._stringSplit(str, delimiter);
return outputArray;
}
};
MyNamespace.Singleton = (function() {
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [, , ]; function privateMethod1() {
...
}
function privateMethod2(args) {
...
} return { // Public members.
publicAttribute1: true,
publicAttribute2: , publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
};
})();
MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here.
// Private members.
var privateAttribute1 = false;
var privateAttribute2 = [, , ]; function privateMethod1() {
...
}
function privateMethod2(args) {
...
} return { // Public members.
publicAttribute1: true,
publicAttribute2: , publicMethod1: function() {
...
},
publicMethod2: function(args) {
...
}
}
} })();
MyNamespace.Singleton = (function() { function constructor() { // All of the normal singleton code goes here.
...
} return {
getInstance: function() {
// Control code goes here.
}
}
})();
MyNamespace.Singleton = (function() { var uniqueInstance; // Private attribute that holds the single instance. function constructor() { // All of the normal singleton code goes here.
...
} return {
getInstance: function() {
if(!uniqueInstance) { // Instantiate only if the instance doesn't exist.
uniqueInstance = constructor();
}
return uniqueInstance;
}
}
})();
MyNamespace.Singleton = (function() {
var objectA = {
method1: function() {
...
},
method2: function() {
...
}
};
var objectB = {
method1: function() {
...
},
method2: function() {
...
}
}; return (someCondition) ? objectA : objectB;
})();
/* SimpleXhrFactory singleton, step 1. */ var SimpleXhrFactory = (function() { // The three branches.
var standard = {
createXhrObject: function() {
return new XMLHttpRequest();
}
};
var activeXNew = {
createXhrObject: function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}
};
var activeXOld = {
createXhrObject: function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}; })(); /* SimpleXhrFactory singleton, step 2. */ var SimpleXhrFactory = (function() { // The three branches.
var standard = {
createXhrObject: function() {
return new XMLHttpRequest();
}
};
var activeXNew = {
createXhrObject: function() {
return new ActiveXObject('Msxml2.XMLHTTP');
}
};
var activeXOld = {
createXhrObject: function() {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}; // To assign the branch, try each method; return whatever doesn't fail.
var testObject;
try {
testObject = standard.createXhrObject();
return standard; // Return this if no error was thrown.
}
catch(e) {
try {
testObject = activeXNew.createXhrObject();
return activeXNew; // Return this if no error was thrown.
}
catch(e) {
try {
testObject = activeXOld.createXhrObject();
return activeXOld; // Return this if no error was thrown.
}
catch(e) {
throw new Error('No XHR object found in this environment.');
}
}
} })();
《javascript设计模式》笔记之第五章:单体模式的更多相关文章
- javascript设计模式学习之十五——装饰者模式
一.装饰者模式定义 装饰者模式可以动态地给某个对象添加一些额外的职责,而不会影响从这个类中派生的其他对象.这种为对象动态添加职责的方式就称为装饰者模式.装饰者对象和它所装饰的对象拥有一致的接口,对于用 ...
- [书籍翻译] 《JavaScript并发编程》第五章 使用Web Workers
本文是我翻译<JavaScript Concurrency>书籍的第五章 使用Web Workers,该书主要以Promises.Generator.Web workers等技术来讲解Ja ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第五章:渲染流水线 学习目标 了解几个用以表达真实场景的标志和2D图像 ...
- Java 设计模式系列(十五)迭代器模式(Iterator)
Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...
- Javascript设计模式理论与实战:工厂方法模式
本文从简单工厂模式的缺点说起,引入工厂方法模式,介绍的工厂方法模式的基本知识,实现要点和应用场景,最后举例进行说明工厂方法模式的应用.在之前的<Javascript设计模式理论与实战:简单工厂模 ...
- JavaScript DOM编程艺术-学习笔记(第五章、第六章)
第五章: 1.题外话:首先大声疾呼,"js无罪",有罪的是滥用js的那些人.js的father 布兰登-艾克,当初为了应付工作,10天就赶出了这个js,事后还说人家js是c语言和s ...
- Javascript设计模式笔记
Javascript是越来越厉害了,一统前后端开发.于是最近把设计模式又看了一遍,顺便做了个笔记,以方便自己和他人共同学习. 笔记连载详见:http://www.meteorcn.net/wordpr ...
- 《LINUX内核设计与实现》读书笔记之第五章
第五章——系统调用 5.1 与内核通信 1.为用户空间提供一种硬件的抽象接口 2.保证系统稳定和安全 3.除异常和陷入,是内核唯一的合法入口. API.POSIX和C库 关于Unix接口设计:提供机制 ...
- Linux内核分析 读书笔记 (第五章)
第五章 系统调用 5.1 与内核通信 1.调用在用户空间进程和硬件设备之间添加了一个中间层.该层主要作用有三个: 为用户空间提供了硬件的抽象接口. 系统调用保证了系统的稳定和安全. 实现多任务和虚拟内 ...
随机推荐
- tomcat启动项目被重新加载,导致资源初始化两遍
之前没有遇到过这个问题,配了三天的项目了,惊人啊!!!各种怪问题全被我赶上了.真有种骂人的冲动. tomcat启动项目时,项目资源被加载两遍. 原因:配置虚拟目录导致,项目被重新加载. <Hos ...
- docker中Ubuntu安装jdk1.8
1.在宿主系统下载所需要的jdk版本的gz文件 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...
- template <typename T>模板类定义
#include "stdafx.h"#include "iostream"#include <ctime>using namespace std; ...
- ubuntu 下编译安装ceph
git clone --recursive https://github.com/ceph/ceph.git cd ceph/ sudo apt-get install libtool sud ...
- ADB命令小结
)adb devices //查看启动的所有设备 )adb kill-server //重启设备 )adb start-server //启动设备 )adb -s emulator-(通过 adb d ...
- 如何正确遍历删除List中的元素,你会吗?
遍历删除List中的元素有很多种方法,当运用不当的时候就会产生问题.下面主要看看以下几种遍历删除List中元素的形式: 1.通过增强的for循环删除符合条件的多个元素 2.通过增强的for循环删除符合 ...
- 一、使用 BeautifulSoup抓取网页信息信息
一.解析网页信息 from bs4 import BeautifulSoup with open('C:/Users/michael/Desktop/Plan-for-combating-master ...
- 【Hadoop】HDFS笔记(三):HDFS的Shell操作
HDFS处理文件的命令和Linux命令差不多,但注意区分大小写. (Linux区分大小写,Windows不区分大小写) 一.fs命令 键入命令"./bin/hadoop fs"将输 ...
- sqlserver——视图
数据库中的视图是一个虚拟表.同真实的表一样,视图包含一系列带有名称的列和行数据,行和列数据用来自由定义视图和查询所引用的表,并且在引用视图时动态产生.本篇将通过一些实例来介绍视图的概念,视图的作用,创 ...
- E20180518-hm
priority n. 优先,优先权; (时间,序上的) 先,前; 优先考虑的事; [数] 优先次序;