设计模式---享元模式(C++实现)

    xiaoxiao2021-03-25  61

    /* 享元模式:通过与其他类共享部分元素减小内存的使用 抽象元素  + 具体元素   共享工厂 享元模式的特点是,复用我们内存中已存在的对象,降低系统创建对象实例 共享对象的内部状态,至于外部状态可以提供接口来和内部数据进行对接处理 */ #include <iostream> #include <string> #include <vector> #include <cassert> using namespace std; class FlyWeight { public: virtual ~FlyWeight(){} virtual void Operation(const string& exState){} string GetInsState(){ return this->_insState; } protected: FlyWeight(string insState) { this->_insState = insState; } private: string _insState; }; class ConFlyweight :public FlyWeight { public: ConFlyweight(string insState) :FlyWeight(insState) { cout << "ConFlyweight Build " << insState << endl; } ~ConFlyweight(){}; void Operation(const string& exState) { cout << this->GetInsState() << endl; cout << exState << endl;//在这里对内部和外部状态进行汇合处理 } };  class FlyWeightFactory { public: FlyWeightFactory(){} ~FlyWeightFactory(){} FlyWeight* GetFlyweight(const string& key) { vector<FlyWeight*>::iterator it = _fly.begin(); for (; it != _fly.end(); it++) { if ((*it)->GetInsState() == key) { cout << "Have Created" << endl; return *it; } } FlyWeight* fn = new ConFlyweight(key); _fly.push_back(fn); return fn; } private: vector<FlyWeight*> _fly; }; void main_12() { FlyWeightFactory *fw = new FlyWeightFactory(); FlyWeight* fw1 = fw->GetFlyweight("Jackill"); FlyWeight* fw2 = fw->GetFlyweight("Rukawa"); FlyWeight* fw3 = fw->GetFlyweight("Jackill"); system("pause");

    }

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力, 由于这个特性,它完成有可能在我们处理一对一数据的时候,在编程上提供快速通道。这里说下map内部数据的组织,map内部自建一颗红黑树(一种非严格意 义上的平衡二叉树),这颗树具有对数据自动排序的功能,所以在map内部所有的数据都是有序的.举例说明什么是一对一的数据映射。比如一个班级中,每个 学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述 1.数据的插入 在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法: 第一种:用insert函数插入pair数据, mapStudent.insert(pair<int, string>(1, “student_one”)); 第二种:用insert函数插入value_type数据,举例 mapStudent.insert(map<int, string>::value_type (1, “student_one”)); 第三种:用数组方式插入数据,举例 mapStudent[1] =  “student_one”; mapStudent[2] =  “student_two”; 用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的, 但是用数组方式就不同了,它可以覆盖以前该关键字对应的值, mapStudent.insert(map<int, string>::value_type (1,“student_one”)); 上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我 们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功: Pair<map<int, string>::iterator, bool> Insert_Pair; Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, “student_one”)); 我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话 Insert_Pair.second应该是true的,否则为false。大家可以用如下程序,看下用数组插入在数据覆盖上的效果 2.map的大小 在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下: Int nSize = mapStudent.size(); 3.数据的遍历 这里也提供三种方法,对map进行遍历 map<int, string>::reverse_iterator  iter; for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++) { Cout<<iter->first<<”   ”<<iter->second<<end;} 4.数据的查找(包括判定这个关键字是否在map中出现) 在这里我们将体会,map在数据插入时保证有序的好处。 要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的 map基本用法。 这里给出三种数据查找方法 第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个, 要么是0,要么是1,出现的情况,当然是返回1了 第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器, 第三种:这个方法用来判定数据是否出现,是显得笨了点,但是,我打算在这里讲解 Lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器) Upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)

    转载请注明原文地址: https://ju.6miu.com/read-38957.html

    最新回复(0)