结构型模式-享元(flyweight)

享元

运用共享技术有效地支持大量细粒度的对象.

实例

main.cc:

#include "clothes_factory.h"
#include "leading_runner_clothes.h"
#include 
using namespace std;

/*
design_pattern:"flyweight"
Each runner club has its own clothes. Each piece of clothing for an object, 
then when the group ran to 100 thousand people, there are 100 thousand objects,
the flyweight pattern can only save a lot of memory resources.
The leading runner of the clothes have their own names, ordinary members of the clothes only the size difference.
*/
int main(){
    ClothesFactory *clothes_factory = new ClothesFactory();
    clothes_factory->GetClothes('S');
    clothes_factory->GetClothes('M');
    clothes_factory->GetClothes('L');
    Clothes *clothes_first = clothes_factory->GetClothes('X');
    Clothes *clothes_second = clothes_factory->GetClothes('X');
    Clothes *leading_clothes_first = new LeadingRunnerClothes();
    Clothes *leading_clothes_second = new LeadingRunnerClothes();

    //check
    printf("clothes_first=%p,clothes_second=%p\n", clothes_first, clothes_second);
    printf("leading_clothes_first=%p,leading_clothes_second=%p\n",leading_clothes_first,leading_clothes_second);

    //clear 
    delete clothes_factory;
    delete leading_clothes_first;
    delete leading_clothes_second;
    system("Pause");
    return 0;
}

ClothesFactory:

//clothes_factory.h
#ifndef HELENDP_SOURCE_CLOTHES_FACTORY_H_
#define HELENDP_SOURCE_CLOTHES_FACTORY_H_
#include "clothes.h"
#include 
using namespace std;
class ClothesFactory{
public:
    ClothesFactory();
    ~ClothesFactory();
    Clothes *GetClothes(int key);
private:
    map<int,Clothes*> map_clothes_;
};
#endif


//clothes_factory.cc
#include "clothes_factory.h"
#include "runner_clothes.h"
#include 
using namespace std;

ClothesFactory::ClothesFactory(){

}

ClothesFactory::~ClothesFactory(){
    map<int,Clothes*>::iterator iterator;
    iterator = map_clothes_.begin();
    while(iterator != map_clothes_.end()){
        delete iterator->second;
        printf("%s:[%c] delete!\n",__func__,iterator->first);
        iterator++;
    }
}

Clothes *ClothesFactory::GetClothes(int key){
    map<int,Clothes*>::iterator iterator = map_clothes_.find(key);

    if(iterator == map_clothes_.end()){
        Clothes *clothes = new RunnerClothes();
        map_clothes_.insert(make_pair(key,clothes));
        cout << "key (" << key << ")" << " is not exist,insert." << endl;
        return clothes;
    }
    else{
        cout << "key (" << key << ")" << " is exist,return object." << endl;
        return iterator->second;
    }
}

Clothes:

//clothes.h
#ifndef HELENDP_SOURCE_CLOTHES_H_
#define HELENDP_SOURCE_CLOTHES_H_

class Clothes{
public:
    Clothes();
    virtual ~Clothes();
};
#endif


//clothes.cc
#include "clothes.h"

Clothes::Clothes(){

}

Clothes::~Clothes(){

}

RunnerClothes:

//runner_clothes.h
#ifndef HELENDP_SOURCE_RUNNER_CLOTHES_H_
#define HELENDP_SOURCE_RUNNER_CLOTHES_H_
#include "clothes.h"

class RunnerClothes : public Clothes{
public:
    RunnerClothes();
    ~RunnerClothes();
};
#endif


//runner_clothes.cc
#include "runner_clothes.h"

RunnerClothes::RunnerClothes(){

}

RunnerClothes::~RunnerClothes(){

}

LeadingRunnerClothes:

//leading_runner_clothes.h
#ifndef HELENDP_SOURCE_LEADING_RUNNER_CLOTHES_H_
#define HELENDP_SOURCE_LEADING_RUNNER_CLOTHES_H_
#include "clothes.h"

class LeadingRunnerClothes : public Clothes{
public:
    LeadingRunnerClothes();
    ~LeadingRunnerClothes();
};
#endif


//leading_runner_clothes.cc
#include "leading_runner_clothes.h"

LeadingRunnerClothes::LeadingRunnerClothes(){

}

LeadingRunnerClothes::~LeadingRunnerClothes(){

}

代码和UML图(EA)工程文件,最后会整理打包上传.

UML类图

结构型模式-享元(flyweight)_第1张图片

结构

  • FlyweightFactory(ClothesFactory):创建并管理flyweight对象.
  • Flyweight(Clothes):抽象享元类.
  • ConcreteFlyweight(RunnerClothes):具体享元类.
  • UnsharedConcreteFlyweight(LeadingRunnerClothes):非共享具体享元类.

优点

  • 享元模式的优点在于它可以极大减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份.
  • 享元模式的外部状态相对独立,而且不会影响其内部状态,从而使得享元对象可以在不同的环境中被共享.

缺点

  • 享元模式使得系统更加复杂,需要分离出内部状态和外部状态,这使得程序的逻辑复杂化.
  • 为了使对象可以共享,享元模式需要将享元对象的状态外部化,而读取外部状态使得运行时间变长.

你可能感兴趣的:(设计模式)