javascript的设计模式实现01之Factory

Factory模式参考图片:


代码:

var Product = function() {};
Product.prototype.operation = function() {};

var ConcreteProduct = function() {
	this.operation = function() {
		console.log("concrete product operation");
	};
};
ConcreteProduct.prototype = new Product;
ConcreteProduct.prototype.constructor = ConcreteProduct;

var Creator = function() {};
Creator.prototype.factoryMethod = function() {};
Creator.prototype.getProduct = function() {
	return this.product;
};
Creator.prototype.setProduct = function(o) {
	this.product = o;
};
Creator.prototype.operation = function() {
	console.log("before operation");
	var product = this.factoryMethod();
	product.operation();
	console.log("end operation");
	return product;
};

var ConcreteFactory = function() {
	this.factoryMethod = function() {
		var o = new ConcreteProduct;
		this.setProduct(o);
		return o;		
	};
};
ConcreteFactory.prototype = new Creator;
ConcreteFactory.prototype.constructor = ConcreteFactory;

var o = new ConcreteFactory;
o.factoryMethod().operation();
o.operation();

例子:

var Phone = function() {};
Phone.prototype.makePhone = function() {
	console.log("make phone start...");
};
Phone.prototype.getInfo = function() {
	console.log("get info start...");
};

var IPhone = function(_sn) {
	this.sn = _sn;
	var Isuper = {
		makePhone: this.makePhone,
		getInfo: this.getInfo
	};
	this.getInfo = function() {
		Isuper.getInfo();
		console.log(this.label + " sn:" + this.sn);
	};
	{
		console.log("iphone start...");
		Isuper.makePhone();
		console.log("iphone end...");
	}
};

IPhone.prototype = new Phone;
IPhone.prototype.constructor = IPhone;
IPhone.prototype.label = "iphone";

var HTC = function(_sn) {
	this.sn = _sn;
	var Isuper = {
		makePhone: this.makePhone,
		getInfo: this.getInfo
	};
	this.getInfo = function() {
		Isuper.getInfo();
		console.log(this.label + " sn:" + this.sn);
	};
	{
		console.log("htc start...");
		Isuper.makePhone();
		console.log("htc end...");
	}
};

HTC.prototype = new Phone;
HTC.prototype.constructor = HTC;
HTC.prototype.label = "htc";

var Factory = function() {};
Factory.prototype.getPhone = function() {
	console.log("factory start...");
};

var IPhoneFactory = function() {
	var ISuper = {
		getPhone: this.getPhone
	};
	this.getPhone = function() {
		ISuper.getPhone();
		console.log("iphone factory start...");
		return new IPhone(new Date().getMilliseconds());
	};
};

IPhoneFactory.prototype = new Factory;
IPhoneFactory.prototype.constructor = IPhoneFactory;

var HTCFactory = function() {
	var ISuper = {
		getPhone: this.getPhone
	};
	this.getPhone = function() {
		ISuper.getPhone();
		console.log("htc factory start...");
		return new HTC(new Date().getMilliseconds());
	};
};

HTCFactory.prototype = new Factory;
HTCFactory.prototype.constructor = HTCFactory;

var o = new HTCFactory;
o.getPhone().getInfo();
o.getPhone().getInfo();
o.getPhone().getInfo();
o = new IPhoneFactory;
o.getPhone().getInfo();
o.getPhone().getInfo();
o.getPhone().getInfo();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值