共享模式(C++实现)

(本博客旨在个人总结回顾)

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;
}

2.3、运行结果:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值