???????????C++???
???????????? ???????[ 2014/8/29 14:20:11 ] ??????????????? ???????
???????н??
??????????????
???????????Σ???ī????????????!?? ????????????????????????????????????????
????UML????£?
????????????????????????????????????????????“?????”???????????????????????????????????????????????????????????????????ò??????????????????????????????????????????????????Factory?????????????????????????????Factory??????ConcreteFactory?н??С?
???????????????????????
????1)??????????? ?????????????????????????ó???????????幤??????????????????????е????
????2)???幤????????????к???????????й?????????ó??????????????????????????
????3)??????????????????????е????????????????
????4)??????????????幤????????????????????????????
??????????????????????????????????????????????????????????????μ???????????????????????1???????C++????У??????????????????????????????????????????????????????????塣
????????????
#include <iostream>
usingnamespace std;
enum SOAPTYPE {SFJ??XSL??NAS};
class soapBase
{
public:
virtual ~soapBase(){};
virtualvoid show() = 0;
};
class SFJSoap:public soapBase
{
public:
void show() {cout<<"SFJ Soap!"<<endl;}
};
class XSLSoap:public soapBase
{
public:
void show() {cout<<"XSL Soap!"<<endl;}
};
class NASSoap:public soapBase
{
public:
void show() {cout<<"NAS Soap!"<<endl;}
};
class FactoryBase
{
public:
virtual soapBase * creatSoap() = 0;
};
class SFJFactory:public FactoryBase
{
public:
soapBase * creatSoap()
{
returnnew SFJSoap();
}
};
class XSLFactory:public FactoryBase
{
public:
soapBase * creatSoap()
{
returnnew XSLSoap();
}
};
class NASFactory:public FactoryBase
{
public:
soapBase * creatSoap()
{
returnnew NASSoap();
}
};
int main()
{
SFJFactory factory1;
soapBase* pSoap1 = factory1.creatSoap();
pSoap1->show();
XSLFactory factory2;
soapBase* pSoap2 = factory2.creatSoap();
pSoap2->show();
NASFactory factory3;
soapBase* pSoap3 = factory3.creatSoap();
pSoap3->show();
delete pSoap1;
delete pSoap2;
delete pSoap3;
return 0;
}
???????н??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11