js: abstractFactory

function Employee(name) {
	this.name = name;
	this.say = function() {
		log.add("I am employee " + name);
	}
}

function EmployeeFactory() {
	this.create = function(name) {
		return new Employee();
	}
}

function Vendor(name) {
	this.name = name;
	this.say = function() {
		log.add("I am vendor " + name);
	}
}

function VendorFactory() {
	this.create = function(name) {
		return new Vendor(name);
	}
}


var log = (function() {
	var log = "";
	return {
		add: function(msg) { log += msg + "\n"},
		show: function() { 
			console.log(log);
			log = "";
		}
	};

}());


function run() {
	var persons = [];
	var employeeFactory = new EmployeeFacotry();
	var vendorFactory = new VendorFactory();

	persons.push(employeeFactory.create("manx"));
	persons.push(employeeFactory.create("bob"));
	persons.push(vendorFactory.create("jack"));
	persons.push(vendorFactory.create("smith"));

	for (obj of persons) {
		obj.say();
	}

	log.show();

}

run();

  

posted @ 2017-05-03 21:37  ax=null  阅读(149)  评论(0编辑  收藏  举报