简单工厂模式是由一个工厂对象决定创建出哪一种产品类的实例。简单工厂模式是工厂模式家族中最简单实用的模式,可以理解为是不同工厂模式的一个特殊实现。
简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一个产品类(这些产品类继承自一个父类或接口)的实例。
JS
JavaScript中没有Interface关键字,但可以用它的特性去模仿,
模拟Interface的实现有如下三种方法:
1.注释法
2.属性检查法
3.鸭式辨型法
这里采用鸭式辨型法
Interface
var Interface = function(name, methods){
if (arguments.length != 2) {
throw new Error("Interface constructor called with " + arguments.length +
"arguments,but expected exactyl 2.")
}
this.name = name;
this.methods = [];
for (var i = 0, len = methods.length; i < len; i++) {
if (typeof methods[i] !== "string") {
throw new Error("Interface constructor expects methods names to bo " +
"passed in asastring.");
}
this.methods.push(methods[i]);
}
}
Interface.ensureImplements = function(object){
if (arguments.length < 2) {
throw new Error("Function Interface.ensureImplements called with " + arguments.length +
"arguments,but expected at least 2.");
}
for (var i = 1, len = arguments.length; i < len; i++) {
var inter_face = arguments[i];
if (inter_face.constructor !== Interface) {
throw new Error("Function Interface.ensureImplementsexpects arguments " +
"two and above to be instances of Interface.");
}
for (var j = 0, methodsLen = inter_face.methods.length; j < methodsLen; j++) {
var method = inter_face.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error("Function Interface.ensureImplements: object " +
"does not implements the " +
inter_face.name +
"interface.Method " +
method +
"was not found.");
}
}
}
}
Creator
var Creator = function(){};
Creator.prototype ={
Product : function(model){
var concreteProduct = null;
switch(model){
case 'A':
concreteProduct = new ConcreteProductA();
break;
case 'B':
concreteProduct = new ConcreteProductB();
break;
case 'C':
concreteProduct = new ConcreteProductC();
break;
};
Interface.ensureImplements(concreteProduct,Product);
return concreteProduct;
}
};
Product
var Product = new Interface("ConcreteProduct",['MethodA','MethodB','MethodC','MethodD']);
ConcreteProduct
var ConcreteProductA =function(){};
ConcreteProductA.prototype= {
MethodA:function(){alert("ConcreteProductA MethodA");},
MethodB:function(){...},
MethodC:function(){...},
MethodD:function(){...}
};
.
.
.
test
var obj = new Creator();
obj.Product("A").MethodA();
Java
Creator
public class Creator {
public Product getProduct(String model) {
switch(model)
{
case "A" : return new ConcreteProductA();
case "B" : return new ConcreteProductB();
case "C" : return new ConcreteProductC();
default:return null;
}
}
}
Product
public interface Product {
void MethodA();
void MethodB();
void MethodC();
}
ConcreteProduct
public class ConcreteProductA implements Product{
public void MethodA() {
System.out.println("ConcreteProductA MethodA");
}
public void MethodB() {
System.out.println("ConcreteProductB MethodB");
}
public void MethodC() {
System.out.println("ConcreteProductC MethodC");
}
}
.
.
.
test
public class test {
public static void main(String[] args){
Creator creator = new Creator();
creator.getProduct("A").MethodA();
}
}
C艹
Creator
class Creator
{
public:
Product* getProduct(char model)
{
switch (model)
{
case 'A':
return new ConcreteProductA();
break;
case 'B':
return new ConcreteProductB();
break;
default:
break;
}
}
};
Product
class Product
{
public: virtual void MethodA() = 0;
public: virtual void MethodB() = 0;
public: virtual void MethodC() = 0;
};
ConcreteProduct
class ConcreteProductA :public Product
{
public:
virtual void MethodA()
{
cout << "ConcreteProductA MethodA" << endl;
}
virtual void MethodB()
{
cout << "ConcreteProductA MethodB" << endl;
}
virtual void MethodC()
{
cout << "ConcreteProductA MethodC" << endl;
}
};
。
。
。
test
int main(int argc, char* argv[])
{
Creator creator;
Product* Product;
Product = creator.getProduct('A');
Product->MethodA();
delete Product;
system("pause");
return 0;
}
C井
Creator
public class Creator
{
public Product getProduct(string model) {
switch (model)
{
case "A":
return new ConcreteProductA();
case "B":
return new ConcreteProductB();
default:
return null;
}
}
}
Product
public interface Product
{
void MethodA();
void MethodB();
void MethodC();
}
ConcreteProduct
class ConcreteProductA : Product
{
public void MethodA()
{
Console.WriteLine("ConcreteProductA MethodA");
}
public void MethodB()
{
Console.WriteLine("ConcreteProductA MethodB");
}
public void MethodC()
{
Console.WriteLine("ConcreteProductA MethodC");
}
}
。
。
。
test
class test
{
static void Main(string[] args)
{
Creator creator = new Creator();
creator.getProduct("A").MethodB();
}
}