设计模式flyweight的C++实现源码

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值