(本博客旨在个人总结回顾)
1、详情:
共享模式:运用共享技术有效支撑大量细粒度的对象。
说明:
优点:大大减少对象的创建,降低系统的内存,使效率提高。
缺点:提高了系统的复杂度,需要分离出外部状态和内部状态,而且外部状态具有固有化的性质,不应该随着内部状态的变化而变化,否则会造成系统的混乱。
2.1、UML类图:
2.2、例子源码
stdafx.h
// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
using namespace std;
// TODO: 在此处引用程序需要的其他头文件
Flyweight.h
#pragma once
class Flyweight
{
public:
Flyweight();
virtual ~Flyweight();
public:
virtual void Operation(int nExtrinsicsState) = 0;
};
Flyweight.cpp
#include "stdafx.h"
#include "Flyweight.h"
Flyweight::Flyweight()
{
}
Flyweight::~Flyweight()
{
}
ConcreteFlyweight.h
#pragma once
#include "Flyweight.h"
class ConcreteFlyweight :
public Flyweight
{
public:
ConcreteFlyweight();
virtual ~ConcreteFlyweight();
public:
void Operation(int nExtrinsicsState);
};
ConcreteFlyweight.cpp
#include "stdafx.h"
#include "ConcreteFlyweight.h"
ConcreteFlyweight::ConcreteFlyweight()
{
}
ConcreteFlyweight::~ConcreteFlyweight()
{
}
void ConcreteFlyweight::Operation(int nExtrinsicsState)
{
cout << "具体Flyweight:" << nExtrinsicsState << endl;
}
UnshareConcreteFlyweight.h
#pragma once
#include "Flyweight.h"
class UnsharedConcreteFlyweight :
public Flyweight
{
public:
UnsharedConcreteFlyweight();
~UnsharedConcreteFlyweight();
public:
void Operation(int nExtrinsicsState);
};
UnshareConcreteFlyweight.cpp
#include "stdafx.h"
#include "UnsharedConcreteFlyweight.h"
UnsharedConcreteFlyweight::UnsharedConcreteFlyweight()
{
}
UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight()
{
}
void UnsharedConcreteFlyweight::Operation(int nExtrinsicsState)
{
cout << "不共享的具体Flyweight:" << nExtrinsicsState << endl;
}
FlyweightFactory.h
#pragma once
#include <map>
#include "ConcreteFlyweight.h"
class FlyweightFactory
{
public:
FlyweightFactory();
~FlyweightFactory();
public:
ConcreteFlyweight* GetFlyweight(string strKey);
private:
std::map<string, ConcreteFlyweight*> m_mapConcreteFlyweight;
};
FlyweightFactory.cpp
#include "stdafx.h"
#include "FlyweightFactory.h"
FlyweightFactory::FlyweightFactory()
{
m_mapConcreteFlyweight["X"] = new ConcreteFlyweight();
m_mapConcreteFlyweight["Y"] = new ConcreteFlyweight();
m_mapConcreteFlyweight["Z"] = new ConcreteFlyweight();
}
FlyweightFactory::~FlyweightFactory()
{
delete m_mapConcreteFlyweight["X"];
delete m_mapConcreteFlyweight["Y"];
delete m_mapConcreteFlyweight["Z"];
m_mapConcreteFlyweight.clear();
}
ConcreteFlyweight* FlyweightFactory::GetFlyweight(string strKey)
{
if (m_mapConcreteFlyweight.find(strKey) != m_mapConcreteFlyweight.end())
{
return m_mapConcreteFlyweight[strKey];
}
else
{
return NULL;
}
}
调用代码
FlyweightPatternMemo.cpp
// FlyweightPatternMemo.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "FlyweightFactory.h"
#include "UnsharedConcreteFlyweight.h"
int _tmain(int argc, _TCHAR* argv[])
{
int nExrinsicState = 1;
FlyweightFactory* pFlyweightFactory = new FlyweightFactory();
Flyweight* pFlyweightX = pFlyweightFactory->GetFlyweight("X");
pFlyweightX->Operation(++nExrinsicState);
Flyweight* pFlyweightY = pFlyweightFactory->GetFlyweight("Y");
pFlyweightY->Operation(++nExrinsicState);
Flyweight* pFlyweightZ = pFlyweightFactory->GetFlyweight("Z");
pFlyweightZ->Operation(++nExrinsicState);
UnsharedConcreteFlyweight* pUFlyweight = new UnsharedConcreteFlyweight();
pUFlyweight->Operation(++nExrinsicState);
system("pause");
return 0;
}