基本概念
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ea0396973f9d3832cbff5310a373479.png#pic_center)
具体案例
不考虑外部状态
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/242be96743e45593553bbcdd62f7363b.png#pic_center)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <crtdbg.h>
#include<unordered_map>
#include<map>
using namespace std;
class WebSite
{
public:
WebSite();
~WebSite();
virtual void Use() = 0;
private:
};
class ConcreteWebSite :public WebSite
{
public:
ConcreteWebSite();
~ConcreteWebSite();
ConcreteWebSite(string name) {
this->name = name;
}
void Use() {
cout << "网站分类:" << name << endl;
}
private:
string name;
};
class WebSiteFactory
{
public:
WebSiteFactory();
~WebSiteFactory();
WebSite* GetWebSiiteCategory(string key) {
if (flyweights.find(key) == flyweights.end()) {
flyweights.emplace(key, new ConcreteWebSite(key));
}
return flyweights[key];
}
int GetWebSiteCount() {
return flyweights.size();
}
private:
map<string, ConcreteWebSite*> flyweights;
};
WebSiteFactory::WebSiteFactory()
{
}
WebSiteFactory::~WebSiteFactory()
{
}
ConcreteWebSite::ConcreteWebSite()
{
}
ConcreteWebSite::~ConcreteWebSite()
{
}
WebSite::WebSite()
{
}
WebSite::~WebSite()
{
}
int main(int argc, char** argv)
{
WebSiteFactory* f = new WebSiteFactory();
WebSite* fx = f->GetWebSiiteCategory("商品上架");
fx->Use();
WebSite* fy = f->GetWebSiiteCategory("商品上架");
fy->Use();
WebSite* fz = f->GetWebSiiteCategory("商品上架");
fz->Use();
WebSite* fm = f->GetWebSiiteCategory("博客");
fm->Use();
WebSite* fn = f->GetWebSiiteCategory("博客");
fn->Use();
WebSite* fq = f->GetWebSiiteCategory("博客");
fq->Use();
cout << "网站总数:" << f->GetWebSiteCount() << endl;
delete f, fx, fy, fz, fn, fm, fq;
return 0;
}
考虑内外部状态
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5ae660664d9049661fc55bc7de0b1874.png#pic_center)
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include <crtdbg.h>
#include<unordered_map>
#include<map>
using namespace std;
class User
{
public:
User();
~User();
User(string name) {
this->name = name;
}
string getName() {
return name;
}
private:
string name;
};
class WebSite
{
public:
WebSite();
~WebSite();
virtual void Use(User user) = 0;
private:
};
class ConcreteWebSite :public WebSite
{
public:
ConcreteWebSite();
~ConcreteWebSite();
ConcreteWebSite(string name) {
this->name = name;
}
void Use(User user) {
cout << "网站分类:" << name << " 用户:" << user.getName() << endl;
}
private:
string name;
};
class WebSiteFactory
{
public:
WebSiteFactory();
~WebSiteFactory();
WebSite* GetWebSiiteCategory(string key) {
if (flyweights.find(key) == flyweights.end()) {
flyweights.emplace(key, new ConcreteWebSite(key));
}
return flyweights[key];
}
int GetWebSiteCount() {
return flyweights.size();
}
private:
map<string, ConcreteWebSite*> flyweights;
};
User::User()
{
}
User::~User()
{
}
WebSiteFactory::WebSiteFactory()
{
}
WebSiteFactory::~WebSiteFactory()
{
}
ConcreteWebSite::ConcreteWebSite()
{
}
ConcreteWebSite::~ConcreteWebSite()
{
}
WebSite::WebSite()
{
}
WebSite::~WebSite()
{
}
int main(int argc, char** argv)
{
WebSiteFactory* f = new WebSiteFactory();
WebSite* fx = f->GetWebSiiteCategory("商品上架");
fx->Use(User("菜鸡"));
WebSite* fy = f->GetWebSiiteCategory("商品上架");
fy->Use(User("张三"));
WebSite* fz = f->GetWebSiiteCategory("商品上架");
fz->Use(User("李牛"));
WebSite* fm = f->GetWebSiiteCategory("博客");
fm->Use(User("牛萌萌"));
WebSite* fn = f->GetWebSiiteCategory("博客");
fn->Use(User("李三多"));
WebSite* fq = f->GetWebSiiteCategory("博客");
fq->Use(User("王小二"));
cout << "网站总数:" << f->GetWebSiteCount() << endl;
delete f, fx, fy, fz, fn, fm, fq;
return 0;
}