您当前的位置: 首页 > 

phymat.nico

暂无认证

  • 1浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

设计模式-结构型-享元

phymat.nico 发布时间:2017-10-07 15:54:05 ,浏览量:1

#pragma once

#ifndef FLYWEIGHT_H 

#define FLYWEIGHT_H 

#include  
#include  

typedef std::string STATE; 

class Flyweight 
{ 
public: 
	virtual ~Flyweight(){} 
	STATE GetIntrinsicState(); 
	virtual void Operation(STATE& ExtrinsicState) = 0; 

protected: 
	Flyweight(const STATE& state) 
		:m_State(state) 
	{ 
	} 

private: 
	STATE m_State; 
}; 

class FlyweightFactory 
{ 
public: 
	FlyweightFactory(){} 
	~FlyweightFactory(); 
	Flyweight* GetFlyweight(const STATE& key); 

private: 
	std::list    m_listFlyweight; 
}; 

class ConcreateFlyweight : public Flyweight 
{ 
public: 
	ConcreateFlyweight(const STATE& state) 
		: Flyweight(state) 
	{ 
	} 
	virtual ~ConcreateFlyweight(){} 
	virtual void Operation(STATE& ExtrinsicState); 
}; 

#endif 

#include "StdAfx.h"
#include "flyweight_impl.h"

#include  

inline STATE Flyweight::GetIntrinsicState() 
{ 
	return m_State; 
} 

FlyweightFactory::~FlyweightFactory() 
{ 
	for (std::list::iterator iter = m_listFlyweight.begin();
		iter != m_listFlyweight.end(); ++iter) 
	{ 
		delete (*iter);
	} 
	m_listFlyweight.clear(); 
} 

Flyweight* FlyweightFactory::GetFlyweight(const STATE& key) 
{
	for (std::list::iterator iter = m_listFlyweight.begin();
		iter != m_listFlyweight.end(); ++iter) 
	{ 
		if ((*iter)->GetIntrinsicState() == key) 
		{ 
			std::cout             
关注
打赏
1659628745
查看更多评论
0.0422s