简单工厂(静态工厂)
#include<iostream>
#include<string>
using namespace std;
//接口
class bird {
public:
virtual void show() = 0;
};
class 大雁 : public bird{
void show() {
cout << "静态工厂:大雁" << endl;
}
};
class 企鹅 : public bird {
void show() {
cout << "静态工厂:企鹅" << endl;
}
};
class 鸭子 : public bird {
void show() {
cout << "静态工厂:鸭子" << endl;
}
};
class Factory {
public:
//静态工厂,直接调用工厂的静态方法,不需要创建工厂类的对象
static bird* createBird(string flag){
if (flag == "大雁") {
return new 大雁();
}
else if (flag == "企鹅") {
return new 企鹅();
}
else if (flag == "鸭子") {
return new 鸭子();
}
}
};
int main() {
bird* b = Factory::createBird("大雁");
b->show();
b = Factory::createBird("企鹅");
b->show();
b = Factory::createBird("鸭子");
b->show();
return 0;
}
工厂模式
#include<iostream>
#include<string>
using namespace std;
//接口
class bird {
public:
virtual void show() = 0;
};
class 大雁 : public bird {
void show() {
cout << "工厂模式:大雁" << endl;
}
};
class 企鹅 : public bird {
void show() {
cout << "工厂模式:企鹅" << endl;
}
};
class 鸭子 : public bird {
void show() {
cout << "工厂模式:鸭子" << endl;
}
};
//工厂接口
class Factory {
public:
virtual bird* createBird() = 0;
};
class 大雁Factory : public Factory{
bird* createBird() {
return new 大雁();
}
};
class 企鹅Factory :public Factory {
bird* createBird() {
return new 企鹅();
}
};
class 鸭子Factory :public Factory {
bird* createBird() {
return new 鸭子();
}
};
int main() {
Factory* factory = new 大雁Factory();
bird* b = factory->createBird();
b->show();
factory = new 企鹅Factory();
b = factory->createBird();
b->show();
factory = new 鸭子Factory();
b = factory->createBird();
b->show();
return 0;
}
抽象工厂模式
工厂模式只有一个维度,而抽象工厂有两个维度
增加一个抽象工厂就相当于是增加了一个产品族
#include<iostream>
#include<string>
using namespace std;
//产品等级接口
class bird {
public:
virtual void show() = 0;
};
class 大雁 : public bird {
void show() {
cout << "工厂模式:大雁" << endl;
}
};
class 企鹅 : public bird {
void show() {
cout << "工厂模式:企鹅" << endl;
}
};
class 鸭子 : public bird {
void show() {
cout << "工厂模式:鸭子" << endl;
}
};
//中国bird
class 中国大雁 : public 大雁 {
void show() {
cout << "中国大雁" << endl;
}
};
class 中国企鹅 :public 企鹅 {
void show() {
cout << "中国企鹅" << endl;
}
};
class 中国鸭子 : public 鸭子 {
void show() {
cout << "中国鸭子" << endl;
}
};
//美国bird
class 美国大雁 : public 大雁 {
void show() {
cout << "美国大雁" << endl;
}
};
class 美国企鹅 : public 企鹅 {
void show() {
cout << "美国企鹅" << endl;
}
};
class 美国鸭子 : public 鸭子 {
void show() {
cout << "美国企鹅" << endl;
}
};
//产品族接口
class Factory {
public:
virtual bird* create大雁() = 0;
virtual bird* create企鹅() = 0;
virtual bird* create鸭子() = 0;
};
class ChinaFactory:public Factory {
bird* create大雁() {
return new 中国大雁();
}
bird* create企鹅() {
return new 中国企鹅();
}
bird* create鸭子() {
return new 中国鸭子();
}
};
class AmericanFactory:public Factory {
bird* create大雁() {
return new 美国大雁();
}
bird* create企鹅() {
return new 美国企鹅();
}
bird* create鸭子() {
return new 美国鸭子();
}
};
int main() {
cout << "中国产品族:" << endl;
Factory* factory = new ChinaFactory();
bird* b = factory->create大雁();
b->show();
b = factory->create企鹅();
b->show();
b = factory->create鸭子();
b->show();
cout << endl;
cout << "美国产品族:" << endl;
factory = new AmericanFactory();
b = factory->create大雁();
b->show();
b = factory->create企鹅();
b->show();
b = factory->create鸭子();
b->show();
}