码迷,mamicode.com
首页 > 编程语言 > 详细

享元模式之C++实现

时间:2014-06-25 09:06:02      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   color   get   


#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;

class Character
{
protected:
    char symbol;
    int size;
    string font;
public:
    Character():symbol(\0), size(0), font(""){}
    void Display()
    {
        cout << "symbol(" << this->symbol << "";
        cout << "size(" << this->size << "";
        cout << "font(" << this->font << "" << endl;
    }
};

class CharacterA : public Character
{
public:
    CharacterA()
    {
        symbol = A;
        size = 10;
        font = "黑体";
    }
};

class CharacterB : public Character
{
public:
    CharacterB()
    {
        symbol = B;
        size = 14;
        font = "宋体";
    }
};

class CharacterC : public Character
{
public:
    CharacterC()
    {
        symbol = C;
        size = 12;
        font = "微软雅黑";
    }
};

class CharacterFactory
{
private:
    map<char, Character*> map_char;
public:
    CharacterFactory()
    {
        map_char.insert(make_pair<char, Character*>(Anew CharacterA));
        map_char.insert(make_pair<char, Character*>(Bnew CharacterB));
        map_char.insert(make_pair<char, Character*>(Cnew CharacterC));
    }

    Character* GetCharacter(char ch)
    {
        map<char, Character*>::iterator it = map_char.find(ch);
        if (it != map_char.end())
        {
            return (Character*)it->second;
        }
        return NULL;
    }
};

int main(void)
{
    CharacterFactory *pFactory = new CharacterFactory;
    char czArray[7] = "AABCAB";
    Character *pChar = NULL;
    int i = 0;
    for (i = 0; i < 6; i++)
    {
        pChar = pFactory->GetCharacter(czArray[i]);
        pChar->Display();
    }
    return 0;

} 

享元模式之C++实现,布布扣,bubuko.com

享元模式之C++实现

标签:style   class   blog   code   color   get   

原文地址:http://www.cnblogs.com/jingmoxukong/p/3806926.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!