JS模式:又一个简单的工厂模式
2024-08-28 07:59:48
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script>
function Car(options){
this.doors = options.doors || 4;
this.state = options.state || "brand new";
this.color = options.color || "silver";
};
function Truck(options){
this.state = options.state || "used";
this.wheelSize = options.wheelSize || "large";
this.color = options.color || "blue";
};
function VehicleFactory(){};
VehicleFactory.prototype.vehicleClass = Car;
VehicleFactory.prototype.createVehicle = function( options ){
if(options.vehicleType == "car"){
this.vehicleClass = Car;
}else{
this.vehicleClass = Truck;
};
return new this.createVehicle(options);
};
var carFactory = new VehicleFactory({
vehicleType : 'truck',
state : 'like new',
color : 'red',
wheelSize : 'small'
});
console.log( carFactory instanceof VehicleFactory);
console.log( carFactory ); function TruckFactory(){};
TruckFactory.prototype = new VehicleFactory();
TruckFactory.prototype.vehicleClass = 'truck'; var truckFactory = new TruckFactory();
console.log( truckFactory instanceof TruckFactory); //抽象 工厂模式---->_->
var AbstractVehicleFactory = (function(){
//定义车辆的类型,用来存储数据
var types = {};
return {
registerVehicle : function(type,vehicle){
types[type] = vehicle;
},
getVehicle : function(type,customizatoins){
var vehicle = types[type];
return (vehicle) ? new topics[type](customizations) : null;
}
}
})();
</script>
</body>
</html>
最新文章
- Java Web技术之Cookie
- struts深入理解之登录示例的源码跟踪
- Mysql命令大全&;&;指定端口登录
- 有哪些可以将网页另存图片的chrome插件?功能类似网页截图
- UVa10723 - Cyborg Genes
- Entity Framework分页扩展
- SpringBoot之常用注解
- spring boot 开始部署报Whitelabel Error Page错解决方法
- ArcGIS案例教程-通过点坐标生成矩形
- KubeCon CloudNativeCon China 2019
- SQL-索引-012
- jQuery的appendTo案例
- vue 本地开发时使用localhost与ip访问
- 关于Android 主题的那些事
- 菜鸟如何使用hanlp做分词的过程记录
- dubbo-常用配置
- Java 操纵XML之创建XML文件
- 详解Java多线程编程中LockSupport类的线程阻塞用法
- vs下C++内存泄露检测
- CSharp程序员学Android开发---3.Android内部元素不填充BUG