结构型模式之享元模式实现

概念

享元模式是构造型模式之一,它通过与其他类似对象共享数据来减少内存占用。

在面向对象系统的设计实现中,创建对象是最为常见的操作。这里面就有一个问题:如果一个应用程序使用了太多的对象,就会造成很大的存储开销。特别是对于大量轻量级(细粒度)的对象,比如在文档编辑器的设计过程中,我们如果为每一字母创建一个对象的话,系统可能会因为大量的对象而造成存储开销的浪费。例如一个字母“a”在文档中出现了10000次,而实际上我们可以让这一万个字母“a”共享一个对象,当然因为在不同的位置可能字母“a”有不同的显示效果(例如字体和大小等设置不同),这种情况我们可以将对象的状态分为“外部状态”和“内部状态”,将可以被共享(不会变化)的状态作为内部状态存储在对象中,而外部对象(例如上面提到的字体、大小等)我们可以在适当的时候将外部对象作为参数传递给对象(例如在显示的时候,将字体、大小等信息传递给对象)。

角色和职责

享元模式
抽象享元角色:所有具体享元类的父类,规定一些需要实现的公共接口;
具体享元角色:抽象享元角色的具体实现类,并实现了抽象享元角色规定的方法;
享元工厂角色:负责创建和管理享元角色。

适用于:是以共享的方式,高效的支持大量的细粒度的对象。

案例

#include <iostream>
#include <string>
#incude <map>
using namespace std;

class Person
{
public:
    Person(string name, int age, int sex)
    {
        this->name = name;
        this->age = age;
        this->sex = sex;
    }
    string getName()
    {
        return name;
    }
    int getAge()
    {
        return age;
    }
    int getSex()
    {
        return sex;
    }

private:
    string name;
    int age;
    int sex;
};

class Teacher:public Person
{
public:
    Teacher(string id, int age, int sex):Person(name,age,sex)
    {
        this->id = id;
    }
    string getId()
    {
        return id;
    }
    void printT()
    {
        cout << "id:" <<id << "\t" << "name:" <<name << "\t" << "age:" <<age << "\t" << "sex:" <<sex << "\t" << endl;
    }

private:
    string id;
}

class TeacherFactory
{
public:
    TeacherFactory()
    {
        m_tpool.empty();
    }
    ~TeacherFactory()
    {
        //在工厂中创建老师节点,在工厂中销毁老师结点
        while(!m_tpool.empty())
        {
            Teacher *tmp = NULL;
            map<string,Teaher *>::iterator it = m_tpool.begin();
            tmp = it->second();
            m_tpool.erase(it);
            delete tmp;
        }
    }
    //通过Teacher的pool,来存放老师结点,在TeacherFactory中创建老师、销毁老师
    Teacher *getTeacher(string tid)
    {
        string name;
        int age;
        int sex;

        Teacher *tmp = NULL;
        map<string,Teacher*>::iterator it = m_tpool.find(tid);
        if(it == m_tpool.end())
        {
            out << "id为: " << tid << " 的老师不存在,系统为你创建该老师,请输入以下信息" <<endl;
            cout << "请输入老师姓名:";
            cin >> name;
            cout << "请输入老师年龄:";
            cin >> age;
            cout << "请输入老师性别 1男 2女:";
            cin >> sex;
            tmp = new Teacher(tid,name,age,sex);
            m_pool.insert(pair<string, Teacher*>(tid,tmp));
        }
        else
        {
            tmp = it->second();
        }

        return tmp;
    }

private:
    map<string, Teacher *>m_tpool;
};

int main(void)
{   
    /*
    Teacher *t1 = new Teacher("001", "小李", 30, 1);
    Teacher *t2 = new Teacher("002", "小张", 30, 1);
    Teacher *t3 = new Teacher("001", "小李", 30, 1);
    Teacher *t4 = new Teacher("004", "小吴", 30, 1);

    cout << "t1 t3的 工号一样,但是也不是同一个人 " << endl;
    delete t1;
    delete t2;
    delete t3;
    delete t4;
    */

    TeacherFactory *teacherFactory = new TeacherFactory();
    Teacher *t1 = teacherFactory->getTeacher("001");
    t1->prinT();

    Teacher *t2 = teacherFactory->getTeacher("001");
    t2->prinT();

    delete teacherFactory;
    delete t2;
    delete t1;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tingzhushaohua

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值