简单工厂:
从前有个工厂,这个工厂能生产两个产品A和B。有一天我来买A,我给工厂说,给我来个A。
工厂老板听见了,哦,你要A啊。我找一下啊。行,我发现我们厂能生产A,你等下哦,我马上给你造处来。
工厂老板将造A的需求抛给生产线,生产线走产品A的线路生产出来A。最后给我。
class Product 产品的基类,提供虚函数接口。 生产线的头
{
public:
virtual void show() = 0;
};
class Product_A : public Product 产品A 生产A线
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product 产品B 生产B线
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
--------------------------------------------------------------------------------
class Factory 工厂老板
{
public:
Product* Create(char i) 返回的是产品的基类
{ 相当于一个基类指针指向了派生类对象。在调用的时候调用派生类的同名覆盖方法。
switch (i) 需要在工厂中确定产品类型
{
case 'A':
return new Product_A; 由于在继承中支持由下向上的转化。所以return子类类型。
break;
case 'B':
return new Product_B;
break;
default:
break;
}
}
};
---------------------------------------------------------------------------------
int main()
{
Factory *factory = new Factory();
factory->Create('A')->show(); 告诉工厂老板,我要一个A产品
factory->Create('B')->show(); 告诉工厂老板,我要一个B产品
return 0;
}
缺点:现在我想要C,我也想要工厂能造C。麻烦了,工厂得造个生产C线来。
违反了开放封闭原则:软件实体(类、模块、函数)可以扩展,但是不可修改。
具体一点,如果要加C,那么首先需要增加C类,这个属于扩展是允许的。但此时工厂类中需要加判断,这个就属于修改,不允许。
工厂方法:
还是从前的哪个工厂老板,你不是要C么,你不是不让我自己选么。那行,我把A、B、C三个子工厂老板给你找来了,你要啥直接问他们要。
class Product
{
public:
virtual void show() = 0;
};
class Product_A : public Product
{
public:
void show()
{
cout << "Product_A" << endl;
}
};
class Product_B : public Product
{
public:
void show()
{
cout << "Product_B" << endl;
}
};
class Product_C : public Product 扩增的!!!!!!
{
public:
void show()
{
cout << "Product_C" << endl;
}
};
-----------------------------------------------------------------------------------
class Factory 大工厂
{
public:
virtual Product* create() = 0;
};
class Factory_A : public Factory 小工厂A 有因为虚函数,访问了父生产线的A子生产线
{
public:
Product* create()
{
return new Product_A;
}
};
class Factory_B : public Factory 小工厂B
{
public:
Product* create()
{
return new Product_B;
}
};
class Factory_C : public Factory 小工厂C 扩增的!!!
{
public:
Product* create()
{
return new Product_C;
}
};
-----------------------------------------------------------------------------------
int main()
{
Factory* productA = new Factory_A(); 问大工厂要产品A 由于虚函数的关系,调用了小工厂A
Factory* productB = new Factory_B(); 问大工厂要产品B
Factory* productC = new Factory_C(); 问大工厂要产品C 扩增的!!!
productA->create()->show();
productB->create()->show();
productC->create()->show();
return 0;
}
扩增的我也写上去了,发现确实没有进行类内函数的修改,只是对类的扩增。
缺点:每增加一种产品,就需要增加一个对象的工厂。开小工厂不要钱的啊。
抽象工厂:
哇,开厂房要钱的啊,那我不开厂房了。就这俩厂房,我多加生产线行不。
//定义抽象类
class product1
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A1 :public product1
{
public:
void show()
{
cout << "product A1" << endl;
}
};
class product_B1 :public product1
{
public:
void show()
{
cout << "product B1" << endl;
}
};
//定义抽象类
class product2
{
public:
virtual void show() = 0;
};
//定义具体类
class product_A2 :public product2
{
public:
void show()
{
cout << "product A2" << endl;
}
};
class product_B2 :public product2
{
public:
void show()
{
cout << "product B2" << endl;
}
};
-------------------------------------------------------------------------------------
class Factory
{
public:
virtual product1 *creat1() = 0;
virtual product2 *creat2() = 0;
};
class FactoryA :public Factory
{
public:
product1 *creat1()
{
return new product_A1();
}
product2 *creat2()
{
return new product_A2();
}
};
class FactoryB :public Factory
{
public:
product1 *creat1()
{
return new product_B1();
}
product2 *creat2()
{
return new product_B2();
}
};
------------------------------------------------------------------------------------
int main()
{
Factory *factoryA = new FactoryA();
factoryA->creat1()->show();
factoryA->creat2()->show();
Factory *factoryB = new FactoryB();
factoryB->creat1()->show();
factoryB->creat2()->show();
return 0;
}
我还没有搞清楚的是,如果抽象工厂在要增加产品A2,不是也将改FactoryA。