本文共 2120 字,大约阅读时间需要 7 分钟。
享元模式(Flyweight Pattern)是一种软件设计模式。
它使用共享物件,用来尽可能减少内存使用量以及分享资讯给尽可能多的相似物件;适合用于当大量物件只是重复因而导致无法令人接受的使用大量内存。
通常物件中的部分状态是可以分享。常见做法是把它们放在外部数据结构,当需要使用时再将它们传递给享元。
享元模式通过共享的方式,高效的支持大量的细粒度的操作。
FlyweightPattern.h
#pragma once#include #include #include
FlyweightPattern.cpp
#include "FlyweightPattern.h"
mainTest.cpp
#include #include #include"FlyweightPattern.h"int main(void){ FlyweightTeacherFactory* flyweightTeacherFactory = new FlyweightTeacherFactory; Teacher* teacher= dynamic_cast (flyweightTeacherFactory->getTeacher("001")); teacher->printT(); Teacher* teacher2 = dynamic_cast (flyweightTeacherFactory->getTeacher("001")); teacher2->printT(); delete teacher; delete teacher2; delete flyweightTeacherFactory; system("pause"); return 0;}
转载地址:http://zemsi.baihongyu.com/