本邮件内容由第三方提供,如果您不想继续收到该邮件,可 点此退订 。
设计模式 -- 建造者模式 - 我爱物联网 阅读原文»
概念
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。
类图
代码
public abstract class Computer {
private String type;
private String cpu;
private String ram;
private String os;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
private String type;
private String cpu;
private String ram;
private String os;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getCpu() {
return cpu;
}
public void setCpu(String cpu) {
this.cpu = cpu;
}
public String getRam() {
return ram;
}
public void setRam(String ram) {
this.ram = ram;
}
public String getOs() {
return os;
}
public void setOs(String os) {
this.os = os;
}
}
创建两种型号的计算机:
public class T410 extends Computer {
private String hardDisk;
public T410() {
this.setType("ThinkPad T410i");
}
public String getHardDisk() {
return hardDisk;
}
public void setHardDisk(String hardDisk) {
this.hardDisk = hardDisk;
}
@Override
public String toString() {
return "T410 [hardDisk=" + hardDisk + ", getType()=" + getType()
+ ", getCpu()=" + getCpu() + ", getRam()=" + getRam()
+ ", getOs()=" + getOs() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + "]";
}
}
private String hardDisk;
public T410() {
this.setType("ThinkPad T410i");
}
public String getHardDisk() {
return hardDisk;
}
public void setHardDisk(String hardDisk) {
this.hardDisk = hardDisk;
}
@Override
public String toString() {
return "T410 [hardDisk=" + hardDisk + ", getType()=" + getType()
+ ", getCpu()=" + getCpu() + ", getRam()=" + getRam()
+ ", getOs()=" + getOs() + ", getClass()=" + getClass()
+ ", hashCode()=" + hashCode() + "]";
}
}
public class X201 extends Computer {
public X201() {
this.setType("Thinkpad X201i");
}
@Override
public String toString() {
return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu()
+ ", getRam()=" + getRam() + ", getOs()=" + getOs()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ "]";
}
}
public X201() {
this.setType("Thinkpad X201i");
}
@Override
public String toString() {
return "X201 [getType()=" + getType() + ", getCpu()=" + getCpu()
+ ", getRam()=" + getRam() + ", getOs()=" + getOs()
+ ", getClass()=" + getClass() + ", hashCode()=" + hashCode()
+ "]";
}
}
在计算机产品类的基础上增加一个ComputerBuilder接口,以及两个的实现类,以便对计算机进行生产:
public interface ComputerBuilder {
void buildCpu();
void buildRam();
void buildHardDisk();
void BuildOs();
Computer getResult();
}
void buildCpu();
void buildRam();
void buildHardDisk();
void BuildOs();
Computer getResult();
}
public class T410Builder implements ComputerBuilder {
private T410 computer = new T410();
@Override
public void buildCpu() {
computer.setCpu("i5");
}
@Override
public void buildRam() {
computer.setRam("4G");
}
@Override
public void buildHardDisk() {
computer.setHardDisk("500G");
}
@Override
public void BuildOs() {
computer.setOs("Win7");
}
@Override
public Computer getResult() {
return computer;
}
}
private T410 computer = new T410();
@Override
public void buildCpu() {
computer.setCpu("i5");
}
@Override
public void buildRam() {
computer.setRam("4G");
}
@Override
public void buildHardDisk() {
computer.setHardDisk("500G");
}
@Override
public void BuildOs() {
computer.setOs("Win7");
}
@Override
public Computer getResult() {
return computer;
}
}
public class X201Builder implements ComputerBuilder {
private X201 computer = new X201();
@Override
public void2014-07-18笔试面试总结(前端) - wishyouhappy 阅读原文»
private X201 computer = new X201();
@Override
public void2014-07-18笔试面试总结(前端) - wishyouhappy 阅读原文»
1. javascript delete
注意:
1. delete 返回返回false表示属性不能被删除,其他情况返回true(删除成功,或者要删除的属性不存在时也返回true)
2. 火狐的console不能完全模拟javascript执行环境,因而结果可能有所区别 例如在火狐下delete function 返回true,在火狐浏览器中执行返回false
3. eval作用域下delete和全局作用域下和函数作用域下有区别
2. 火狐的console不能完全模拟javascript执行环境,因而结果可能有所区别 例如在火狐下delete function 返回true,在火狐浏览器中执行返回false
3. eval作用域下delete和全局作用域下和函数作用域下有区别
看下面例子:
//delete function
function a(){alert(1);};
console.log(delete a); //false
console.log(a); //function a(){alert(1);}
function a(){alert(1);};
console.log(delete a); //false
console.log(a); //function a(){alert(1);}
//delete object
var b = {aa:1,bb:1};
console.log(delete b);//false
console.log(b);//Object {aa: 1, bb: 1}
var b = {aa:1,bb:1};
console.log(delete b);//false
console.log(b);//Object {aa: 1, bb: 1}
//delete new added attribute of object
var c = {aa:1,bb:1};
console.log(delete c.aa); //true
console.log(c); //Object {bb: 1}
var b={a:1,b:2,c:3};
delete b.d; //true
var c = {aa:1,bb:1};
console.log(delete c.aa); //true
console.log(c); //Object {bb: 1}
var b={a:1,b:2,c:3};
delete b.d; //true
//delete the global member
window.aa = 1;
console.log(delete aa); //true
console.log(aa); //ReferenceError: aa is not defined
console.log(delete isNaN); //true
console.log(isNaN(11)); //ReferenceError: isNaN is not defined
console.log(delete Array); //true
var aa = new Array();//ReferenceError: Array is not defined
window.aa = 1;
console.log(delete aa); //true
console.log(aa); //ReferenceError: aa is not defined
console.log(delete isNaN); //true
console.log(isNaN(11)); //ReferenceError: isNaN is not defined
console.log(delete Array); //true
var aa = new Array();//ReferenceError: Array is not defined
//delete variable in function context
function a(){
var c=1;
console.log(c); //1
console.log(delete c); //false
console.log(c); //1
};
a();
function a(){
var c=1;
console.log(c); //1
console.log(delete c); //false
console.log(c); //1
};
a();
//delete in eval context
eval('var a= 1');
delete a; //true
console.log(a); //ReferenceError: a is not defined
eval('function b(){alert(1);};');
delete b; //true
console.log(b); //ReferenceError: b is not defined
eval('var a= 1');
delete a; //true
console.log(a); //ReferenceError: a is not defined
eval('function b(){alert(1);};');
delete b; //true
console.log(b); //ReferenceError: b is not defined
//delete the length of the function and Array
function a(){};
console.log(delete a.length); //false
var aa = [1, 2];
console.log(delete aa.length); //false
//delete the parameter
function cc(c,d){
console.log(delete c);//false
};
cc();
function a(){};
console.log(delete a.length); //false
var aa = [1, 2];
console.log(delete aa.length); //false
//delete the parameter
function cc(c,d){
console.log(delete c);//false
};
cc();
总结:
1. 变量、函数、属性等是否能删除与DontDelete属性有关,如下具有DontDelete属性:
1) 非eval作用域下var 申明的变量
2) 非eval作用域下函数名申明的函数
3) 对象的内置属性,例如 Function对象的length,数组的length
4) 函数的形参
不具有DontDelete属性:
1)对象新增加的属性,包含window对象下新增的属性
2)eval中创建的对象或者是函数
1) 非eval作用域下var 申明的变量
2) 非eval作用域下函数名申明的函数
3) 对象的内置属性,例如 Function对象的length,数组的length
4) 函数的形参
不具有DontDelete属性:
1)对象新增加的属性,包含window对象下新增的属性
2)eval中创建的对象或者是函数
可参考http://perfectionkills.com/understanding-delete/,很长,不过讲的很详细,
2. this的理解及作用域绑定
题目很长,记不清了,大致如下:
var a = 1;
function c(){
console.log(this.a);
console.log(a);
this.a = 3;
}
var b = {
a: 1,
c: function(){
console.log(a);
a = 5;
console.log(this.a);
}
};
c(); /* this.a:1 a: 1
this.a中this为window, a为全局a,
第三句this.a执行完成后全局a值变为3
*/
new c(); /*this.a: undefined
a: 3(上一步才c()执行完成后a变为3)
*/
b.c(); /* a: 3, this.a: 1
a为全局的a,第二步a=5执行完成后全局a变为5,
this.a 为 b.a 值为1
*/
var cc = b.c; /*
将cc引用b.c
*/
cc(); /* a: 5 this.a: 5
注意此处cc 在window context下执行, 丢失了a property
因而此处的this.a 引用的是window下的a
*/
function c(){
console.log(this.a);
console.log(a);
this.a = 3;
}
var b = {
a: 1,
c: function(){
console.log(a);
a = 5;
console.log(this.a);
}
};
c(); /* this.a:1 a: 1
this.a中this为window, a为全局a,
第三句this.a执行完成后全局a值变为3
*/
new c(); /*this.a: undefined
a: 3(上一步才c()执行完成后a变为3)
*/
b.c(); /* a: 3, this.a: 1
a为全局的a,第二步a=5执行完成后全局a变为5,
this.a 为 b.a 值为1
*/
var cc = b.c; /*
将cc引用b.c
*/
cc(); /* a: 5 this.a: 5
注意此处cc 在window context下执行, 丢失了a property
因而此处的this.a 引用的是window下的a
*/
此处引申:针对上面b.c情况,如何绑定作用域?
没有评论:
发表评论