1、共享元接口类Flyweight
#ifndef FLYWEIGHT_H
#define FLYWEIGHT_H
class Flyweight
{
public:
//Flyweight();
//virtual ~Flyweight();
virtual void Operation(char*) = 0;
};
#endif
2、共享元实现类FlyweightImplA定义
#ifndef FLYWEIGHTIMPLA_H
#define FLYWEIGHTIMPLA_H
#include <string>
#include "Flyweight.h"
using namespace std;
class FlyweightImplA : public Flyweight
{
public:
FlyweightImplA(string);
~FlyweightImplA();
void Operation(char*);
private:
string m_name;
};
#endif
3、共享元实现类FlyweightImplA实现
#include <stdio.h>
#include "FlyweightImplA.h"
FlyweightImplA::FlyweightImplA(string sname) : m_name(sname)
{
}
FlyweightImplA::~FlyweightImplA()
{
}
void FlyweightImplA::Operation(char* mess)
{
printf("FlyweightImplA Name: %s Mess: %s/n", m_name.c_str(), mess);
}
4、共享元实现类FlyweightImplB定义
#ifndef FLYWEIGHTIMPLB_H
#define FLYWEIGHTIMPLB_H
#include "Flyweight.h"
class FlyweightImplB : public Flyweight
{
public:
FlyweightImplB(int);
~FlyweightImplB();
void Operation(char*);
private:
int m_number;
};
#endif
5、共享元实现类FlyweightImplB实现
#include <stdio.h>
#include "FlyweightImplB.h"
FlyweightImplB::FlyweightImplB(int num) : m_number(num)
{
}
FlyweightImplB::~FlyweightImplB()
{
}
void FlyweightImplB::Operation(char* mess)
{
printf("FlyweightImplB Number: %d Mess: %s/n" , m_number, mess);
}
6、共享元缺省实现类FlyweightImplDefault定义
#ifndef FLYWEIGHTIMPLDEFAULT_H
#define FLYWEIGHTIMPLDEFAULT_H
#include "Flyweight.h"
class FlyeweightImplDefault :public Flyweight
{
public:
FlyeweightImplDefault();
~FlyeweightImplDefault();
void Operation(char*);
};
#endif
7、共享元缺省实现类FlyweightImplDefault实现
#include <stdio.h>
#include "FlyweightImplDefault.h"
FlyeweightImplDefault::FlyeweightImplDefault()
{
}
FlyeweightImplDefault::~FlyeweightImplDefault()
{
}
void FlyeweightImplDefault::Operation(char* mess)
{
printf("FlyeweightImplDefault Mess: %s/n", mess);
}
8、获取共享元的工厂类FlyweightFactory定义
#ifndef FLYWEIGHTFACTORY_H
#define FLYWEIGHTFACTORY_H
#pragma warning(disable : 4786)
#include <map>
#include <string>
#include "Flyweight.h"
using namespace std;
class FlyweightFactory
{
public:
Flyweight* GetFlyweight(string);
private:
Flyweight* CreateFlyweight(string);
map<string, Flyweight*> flyMap;
};
#endif
9、获取共享元的工厂类FlyweightFactory实现
#include "FlyweightFactory.h"
#include "FlyweightImplA.h"
#include "FlyweightImplB.h"
#include "FlyweightImplDefault.h"
typedef pair<string, Flyweight*> Int_Pair;
Flyweight* FlyweightFactory::GetFlyweight(string key)
{
Flyweight* fRes = NULL;
map<string, Flyweight*>::const_iterator mkey;
mkey = flyMap.find(key);
if(mkey == flyMap.end())
{
fRes = CreateFlyweight(key);
}
else
{
fRes = mkey->second;
}
return fRes;
}
Flyweight* FlyweightFactory::CreateFlyweight(string key)
{
Flyweight* fRes = NULL;
char* cstrA = "FlyweightA";
char* cstrB = "FlyweightB";
if(!stricmp(key.c_str(),cstrA))
{
fRes = new FlyweightImplA(key);
}
else if(!stricmp(key.c_str(),cstrB))
{
fRes = new FlyweightImplB(2);
}
else
{
key = "Defualt";
map<string, Flyweight*>::const_iterator mkey = flyMap.find(key);
if(mkey == flyMap.end())
{
fRes = new FlyeweightImplDefault();
}
else
{
fRes = mkey->second;
}
}
flyMap.insert(Int_Pair(key,fRes));
return fRes;
}
10、客户使用类Client
#include "Flyweight.h"
#include "FlyweightFactory.h"
int main()
{
FlyweightFactory* ff = new FlyweightFactory();
Flyweight* f1 = ff->GetFlyweight("FlyweightA");
printf("First get FlyweightA Address: %x/n", f1);
f1->Operation("first get FlyweightA");
f1 = ff->GetFlyweight("FlyweightB");
printf("first get FlyweightB Address: %x/n", f1);
f1->Operation("first get FlyweightB");
Flyweight* f2 = ff->GetFlyweight("FlyweightA");
printf("second get FlyweightA Address: %x/n", f2);
f2->Operation("second get FlyweightA");
f2 = ff->GetFlyweight("FlyweightB");
printf("second get FlyweightB Address: %x/n", f2);
f2->Operation("second get FlyweightB");
return 0;
}