Flyweight模式

在开发时,如果创建很多对象,就会造成很大的内存开销,特别是大量轻量级(细粒度)的对象,还会造成内存碎片。Flyweight模式就是运用共享技术,有效支持大量细粒度对象的设计模式。

其类结构图如下:
这里写图片描述

在FlyweightFactory中有一个管理、存储对象的对象池,当调用GetFlyweight时会首先遍历对象池,如果已存在,则返回,否则创建新对象添加到对象池中。有些对象可能不想被共享,那么就使用UnshareConcreteFlyweight。

实现:
//Flyweight.h

//Flywight.h

#ifndef _FLYWEIGHT_H_
#define _FLYWEIGHT_H_
#include<string>
using std::string;

class Flyweight
{
public:
    virtual ~Flyweight();
    virtual void Operation(const string& extrinsicState);
    string GetIntrinsicState();
protected:
    Flyweight(string intrinsicState);
private:
    string _intrinsicState;
};

class ConcreteFlyweight :public Flyweight
{
public:
    ConcreteFlyweight(string intrinsicState);
    ~ConcreteFlyweight();
    void Operation(const string& extrinsicState);
};
#endif

//Flyweight.cpp

//Flyweight.cpp

#include"Flyweight.h"
#include<iostream>
using std::cout;

Flyweight::Flyweight(string intrinsicState)
{
    _intrinsicState = intrinsicState;
}
Flyweight::~Flyweight()
{}
void Flyweight::Operation(const string& extrinsicState)
{}
string Flyweight::GetIntrinsicState()
{
    return _intrinsicState;
}

ConcreteFlyweight::ConcreteFlyweight(string intrinsicState) :Flyweight(intrinsicState)
{
    cout << "ConcreteFlyweight Build..." << std::endl;
}
ConcreteFlyweight::~ConcreteFlyweight()
{}
void ConcreteFlyweight::Operation(const string& extrinsicState)
{
    cout << "ConcreteFlyweight:内蕴[" << this->GetIntrinsicState() << "]外蕴[" 
        << extrinsicState << "]" << std::endl;
}

//FlyweightFactory.h

//FlyweightFactory.h

#ifndef _FLYWEIGHTFACTORY_H_
#define _FLYWEIGHTFACTORY_H_

#include"Flyweight.h"
#include<string>
#include<vector>
using std::string;
using std::vector;
class FlyweightFactory
{
public:
    FlyweightFactory();
    ~FlyweightFactory();
    Flyweight* GetFlyweight(const string& key);
private:
    vector<Flyweight*> _fly;

};
#endif

//FlyweightFactory.cpp

//FlyweightFactory.cpp

#include"FlyweightFactory.h"
#include<iostream>
#include<string>
#include<cassert>
using std::string;
using std::cout;

FlyweightFactory::FlyweightFactory()
{}
FlyweightFactory::~FlyweightFactory()
{}
Flyweight* FlyweightFactory::GetFlyweight(const string& key)
{
    vector<Flyweight*>::iterator it = _fly.begin();
    for (; it != _fly.end(); it++)
    {
        if ((*it)->GetIntrinsicState() == key)
        {
            cout << "already create by users..." << std::endl;
            return *it;
        }
    }

    Flyweight* fn = new ConcreteFlyweight(key);
    _fly.push_back(fn);
    return fn;
}

//main.cpp

#include"Flyweight.h"
#include"FlyweightFactory.h"
int main()
{
    FlyweightFactory* fc = new FlyweightFactory();
    Flyweight* fw1 = fc->GetFlyweight("hello");
    Flyweight* fw2 = fc->GetFlyweight("world");
    Flyweight* fw3 = fc->GetFlyweight("hello");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是常见的Java设计模式: 1. 工厂模式(Factory Pattern) 2. 单例模式(Singleton Pattern) 3. 建造者模式(Builder Pattern) 4. 原型模式(Prototype Pattern) 5. 适配器模式(Adapter Pattern) 6. 桥接模式(Bridge Pattern) 7. 组合模式(Composite Pattern) 8. 装饰器模式(Decorator Pattern) 9. 外观模式(Facade Pattern) 10. 享元模式Flyweight Pattern) 11. 代理模式(Proxy Pattern) 12. 观察者模式(Observer Pattern) 13. 中介者模式(Mediator Pattern) 14. 命令模式(Command Pattern) 15. 访问者模式(Visitor Pattern) 16. 解释器模式(Interpreter Pattern) 17. 迭代器模式(Iterator Pattern) 18. 策略模式(Strategy Pattern) 19. 模板方法模式(Template Method Pattern) 20. 职责链模式(Chain of Responsibility Pattern) 21. State 模式(状态模式) 22. Memento 模式(备忘录模式) 23. Builder 模式(建造者模式) 24. Composite 模式(组合模式) 25. Command 模式(命令模式) 26. Adapter 模式(适配器模式) 27. Proxy 模式(代理模式) 28. Bridge 模式(桥接模式) 29. Decorator 模式(装饰器模式) 30. Facade 模式(外观模式) 31. Flyweight 模式(享元模式) 32. Interpreter 模式(解释器模式) 33. Iterator 模式(迭代器模式) 34. Mediator 模式(中介者模式) 35. Memento 模式(备忘录模式) 36. Observer 模式(观察者模式) 37. Prototype 模式(原型模式) 38. Singleton 模式(单例模式) 39. Strategy 模式(策略模式) 40. Template Method 模式(模板方法模式) 41. Visitor 模式(访问者模式

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值