[设计模式]Flyweight享元模式

问题

如果一个应用程序使用了大量的对象,二这些对象造成了很大的存储开销就应该考虑应用享元模式。

Flyweight享元模式

运用共享技术有效地支持大量细粒度的对象(对于C++来说就是共用一个内存块啦,对象指针指向同一个地方)

[设计模式]Flyweight享元模式_第1张图片

FlyweightFactory享元工厂:用来创建并管理Flyweight对象。它主要是用来确保合理地共享Flyweight,当用户请求一个Flyweight时,FlyweightFactory对象提供一个已创建的实例或创建一个(如果不存在)

Flyweight:所有具体享元类的超类或接口,通过这个接口,Flyweight可以接受并作用于外部状态。

ConcreteFlyweight:继承Flyweight超类或实现Flyweight接口,并为内部状态增加存储空间

UnshareConcreteFlyweight:指那些不需要共享的Flyweight子类。因为Flyweight接口共享成为可能,但它并不强制共享。

Flyweight模式中有一个类似Factory模式的对象构造工厂FlyweightFactory,当客户程序员(Client)需要一个对象时候就会向FlyweightFactory发出请求对象的消息GetFlyweight()消息,FlyweightFactory拥有一个管理,存储对象的”仓库”(或者叫对象池,list实现)GetFlyweight()消息会遍历对象池中的对象,如果已经存在则直接返回给Client,否则创建一个新的对象返回给Client。当然可能也有不想被共享的对象(UnshareConcreteFlyweight)

小demo

Flyweight.h

/********************************************************************
	created:	2006/07/26
	filename: 	FlyWeight.h
	author:		李创
                http://www.cppblog.com/converse/

	purpose:	FlyWeight模式的演示代码
*********************************************************************/

#ifndef FLYWEIGHT_H
#define FLYWEIGHT_H

#include <string>
#include <list>

typedef std::string STATE;

class Flyweight
{
public:
	virtual ~Flyweight(){}

    inline STATE GetIntrinsicState(){return m_State;}
	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<Flyweight*>	m_listFlyweight;
};

class ConcreateFlyweight : public Flyweight
{
public:
	ConcreateFlyweight(const STATE& state) : Flyweight(state)	{}
	virtual ~ConcreateFlyweight(){}

	virtual void Operation(STATE& ExtrinsicState);
};

#endif

Flyweight.cpp

/********************************************************************
	created:	2006/07/26
	filename: 	FlyWeight.cpp
	author:		李创
                http://www.cppblog.com/converse/

	purpose:	FlyWeight模式的演示代码
*********************************************************************/

#include "FlyWeight.h"
#include <iostream> 
FlyweightFactory::~FlyweightFactory()
{ 
    std::list<Flyweight*>::iterator iter1,iter2,temp;
    for (iter1=m_listFlyweight.begin(),iter2=m_listFlyweight.end();iter1!=iter2;iter1++)
    {
        temp=iter1; 
        delete(*temp);
    }
    m_listFlyweight.clear();
}

Flyweight* FlyweightFactory::GetFlyweight(const STATE& key)
{ 
    std::list<Flyweight*>::iterator iter1;
    for (iter1=m_listFlyweight.begin();iter1!=m_listFlyweight.end();++iter1)
    {
        if ((*iter1)->GetIntrinsicState()==key)
        {
            std::cout<<"The Flyweight:"<<key<<"already exits"<<std::endl;
            return (*iter1);
        }
    }
    std::cout<<"create a new flyweight:"<<key<<std::endl;
    Flyweight* flyweight=new ConcreateFlyweight(key);
    m_listFlyweight.push_back(flyweight);
    return flyweight;
}

void ConcreateFlyweight::Operation(STATE& ExtrinsicState)
{
    std::cout<<"ConcreateFlyweight: 內蘊["<<GetIntrinsicState()<<"]外蘊["<<ExtrinsicState<<"]"<<std::endl;
}

main.cpp

#include "FlyWeight.h"

int main()
{
	FlyweightFactory flyweightfactory;
	flyweightfactory.GetFlyweight("hello");
	flyweightfactory.GetFlyweight("world");
	flyweightfactory.GetFlyweight("hello");

	system("pause");
	return 0;
}

代码分析:

使用list链表来保存这些可以被共享的对象,需要使用的时候就到链表中查询是不是已经存在了,如果不存在就初始化一个,然后返回这个对象的指针。

Flyweight模式和Factory模式也经常混用。

State模式和Strategy模式中会产生很多的对象,因此我们可以通过Flyweight模式来解决这个问题。

你可能感兴趣的:(flyweight)