<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!-- 引入的核心js公共文件 -->
<script type=text/javascript charset=utf-8 src=../commons/../commons/CommonUtil.js></script>
<script type=text/javascript charset=utf-8>
//既可以当成java的类用,也可以当成c++函数用
function CarShop(){};//卖车类,没有工厂模式
CarShop.prototype = {
constructor : CarShop ,
sellCar:function(type){
var car ; // 声明一个变量
switch(type){
case 'Benz': car = new Benz(); break;
case 'Bmw' : car = new Bmw(); break;
case 'Audi': car = new Audi(); break;
default: 'not buy it ';
}
BH.Interface.ensureImplements(car ,CarInterface);//接口检测,当函数用BH.Interface.ensureImplements = function(object){},java的接口检测是虚拟机做的,js接口检测是我们自己写的。
return car ;
}
};
// 接口对象的实例,当类用BH.Interface = function(name,methods){}
var CarInterface = new BH.Interface('CarInterface' ,['start','run']);
// SuperClass implements CarInterface
function BaseCar(){};
BaseCar.prototype = {//父类方法都在原型对象中
constructor:BaseCar ,
start:function(){
alert(this.constructor.name + ' ..start');//this是调用方法的子类,java多态(通过父类方法里面的this实现)
},
run:function(){
alert(this.constructor.name + ' ..run');
}
};
function Benz(){};
BH.extend(Benz,BaseCar); //BH.extend=function(sub ,sup){},当函数用
Benz.prototype.driveBenz = function(){alert('Benz..drive')};
//Benz.prototype.run = function(){alert('Benz..run')};
function Bmw(){};
BH.extend(Bmw,BaseCar);
Bmw.prototype.driveBmw = function(){alert('Bmw..drive')};
//Bmw.prototype.run = function(){alert('Bmw..run')};
function Audi(){};
BH.extend(Audi,BaseCar);
Audi.prototype.driveAudi = function(){alert('Audi..drive')};
//Audi.prototype.run = function(){alert('Audi..run')};
var shop = new CarShop();
var car = shop.sellCar('Benz');
car.start();
car.run();
car.driveBenz();
var car2 = shop.sellCar('Bmw');
car2.start();
car2.run();
car2.driveBmw();
</script>
</head>
<body>
</body>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!-- 引入的核心js公共文件 -->
<script type=text/javascript charset=utf-8 src=../commons/../commons/CommonUtil.js></script>
<script type=text/javascript charset=utf-8>
// 万事万物都是Object : 卖车的商店 -> 卖车 -> 生产车 -> Factory
function CarShop(){};// 商店
CarShop.prototype = {
constructor : CarShop ,
sellCar:function(type){
/* 面向对象 */
// 销售人员...
var car = CarFactory.createCar(type);//工厂
// 保险、相关的售后服务
return car ;
}
};
//工厂,单体模式
var CarFactory = {
createCar:function(type){
var car ; // 声明一个变量
switch(type){
case 'Benz': car = new Benz(); break;
case 'Bmw' : car = new Bmw(); break;
case 'Audi': car = new Audi(); break;
default: 'not buy it ';
}
//检验接口 交给工厂
BH.Interface.ensureImplements(car ,CarInterface);
return car ;
}
};
// 接口对象的实例
var CarInterface = new BH.Interface('CarInterface' ,['start','run']);
// SuperClass implements CarInterface
function BaseCar(){};
BaseCar.prototype = {
constructor:BaseCar ,
start:function(){
alert(this.constructor.name + ' ..start');
},
run:function(){
alert(this.constructor.name + ' ..run');
}
};
// Class benz bmw audi (都是车)
// 注意关键问题:子类先继承父类 子类在扩展子类自己特有的方法
function Benz(){};
BH.extend(Benz,BaseCar);
Benz.prototype.driveBenz = function(){alert('Benz..drive')};
//Benz.prototype.run = function(){alert('Benz..run')};
function Bmw(){};
BH.extend(Bmw,BaseCar);
Bmw.prototype.driveBmw = function(){alert('Bmw..drive')};
//Bmw.prototype.run = function(){alert('Bmw..run')};
function Audi(){};
BH.extend(Audi,BaseCar);
Audi.prototype.driveAudi = function(){alert('Audi..drive')};
//Audi.prototype.run = function(){alert('Audi..run')};
var shop = new CarShop();
var car = shop.sellCar('Benz');
car.start();
car.run();
car.driveBenz();
var car2 = shop.sellCar('Bmw');
car2.start();
car2.run();
car2.driveBmw();
</script>
</head>
<body>
</body>
</html>