⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flyweight.cpp

📁 设计模式的VC例子
💻 CPP
字号:
/*
设计模式例程
根据有关例程综合而成

石银峰于2002/04/17在杭州整理
*/
/*
意图 
	运用共享技术有效地支持大量细粒度的对象。 
适用性 
	一个应用程序使用了大量的对象。 
	完全由于使用大量的对象,造成很大的存储开销。 
	对象的大多数状态都可变为外部状态。 
	如果删除对象的外部状态,那么可以用相对较少的共享对象取代很多组对象。 
	应用程序不依赖于对象标识。由于F l y w e i g h t 对象可以被共享,对于概念上明显有别的对象,标识测试将返回真值。  
*/

/*
	GlyphFactory -------------> 	Glyph -------------------------> GlyphContext ---> BTree
		:							  |	+-------+
		:...Glay集合				  |			|
		:...Create(Glay)			  |			|
									  Row  -----+
									  Column  --+
									  Charactor
*/
#include "../public/defs.H"

class Window;
class GlyphContext;

/*****************************************************************************/
// Font
class Font {
public:
	  Font(char* a){
		  output("Font::Font(char* a)\n");
	  };
};

class BTree{
	// implement 
};
/*
*/
/*****************************************************************************/
// Glyph
class Glyph {
public:
    virtual ~Glyph();

    virtual void Draw(Window*, GlyphContext&);
/*
*/
    virtual void SetFont(Font*, GlyphContext&);
    virtual Font* GetFont(GlyphContext&);
/*
*/
    virtual void First(GlyphContext&);
    virtual void Next(GlyphContext&);
    virtual bool IsDone(GlyphContext&);
    virtual Glyph* Current(GlyphContext&);
/*
*/
    virtual void Insert(Glyph*, GlyphContext&);
    virtual void Remove(GlyphContext&);
protected:
    Glyph();
};

Glyph::Glyph(){
	output("Glyph::Glyph()\n");
}
Glyph::~Glyph(){
	output("Glyph::~Glyph()\n");
}

void Glyph::Draw(Window*, GlyphContext&){
	output("void Glyph::Draw(Window*, GlyphContext&)\n");
}
/*
*/
void Glyph::SetFont(Font*, GlyphContext&){
	output("void Glyph::SetFont(Font*, GlyphContext&)\n");
}
Font* Glyph::GetFont(GlyphContext&){
	output("Font* Glyph::GetFont(GlyphContext&)\n");
	return NULL;
}
/*
*/
void Glyph::First(GlyphContext&){
	output("void Glyph::First(GlyphContext&)\n");
}
void Glyph::Next(GlyphContext&){
	output("void Glyph::Next(GlyphContext&)\n");
}
bool Glyph::IsDone(GlyphContext&){
	output("bool Glyph::IsDone(GlyphContext&)\n");
	return 0;
}
Glyph* Glyph::Current(GlyphContext&){
	output("Glyph* Glyph::Current(GlyphContext&)\n");
	return NULL;
}
/*
*/
void Glyph::Insert(Glyph*, GlyphContext&){
	output("void Glyph::Insert(Glyph*, GlyphContext&)\n");
}
void Glyph::Remove(GlyphContext&){
	output("void Glyph::Remove(GlyphContext&)\n");
}

/*****************************************************************************/
/* Character
*/
class Character : public Glyph {
public:
    Character(char);

    virtual void Draw(Window*, GlyphContext&);
private:
    char _charcode;
};
Character::Character(char c) : _charcode(c) {
	output("Character::Character(char)\n");
}

void Character::Draw(Window* w, GlyphContext& gc){
	output("void Character::Draw(Window*, GlyphContext&)\n");
	cout << _charcode;
}


/*****************************************************************************/
/* GlyphContext
*/
class GlyphContext {
public:
    GlyphContext();
    virtual ~GlyphContext();
/*
*/
    virtual void Next(int step = 1);
    virtual void Insert(int quantity = 1);
/*
*/
    virtual Font* GetFont();
    virtual void SetFont(Font*, int span = 1);
private:
    int _index;
    BTree* _fonts;
};
GlyphContext::GlyphContext(){
	output("GlyphContext::GlyphContext()\n");
}
GlyphContext::~GlyphContext(){
	output("GlyphContext::~GlyphContext()\n");
}
/*
*/
void GlyphContext::Next(int step){
	output("void GlyphContext::Next(int step)\n");
}
void GlyphContext::Insert(int quantity){
	output("void GlyphContext::Insert(int quantity)\n");
}
/*
*/
Font* GlyphContext::GetFont(){
	output("Font* GlyphContext::GetFont()\n");
	return NULL;
}
void GlyphContext::SetFont(Font* f, int span){
	output("void GlyphContext::SetFont(Font*, int span)\n");
}

/*
*/
void dummy () {
	output("void dummy () \n");
	/*
	*/
	GlyphContext gc;
	Font* times12 = new Font("Times-Roman-12");
	Font* timesItalic12 = new Font("Times-Italic-12");
	// ...

	gc.SetFont(times12, 6);
	/*
	*/
	gc.Insert(6);
	gc.SetFont(timesItalic12, 6);
	/*
	*/
}

/*****************************************************************************/
/* Row
*/
class Row  : Glyph{
private:
	Glyph* data;
};

/*****************************************************************************/
// Golumn
class Column : Glyph{
private:
	Glyph* data;
};
/*
*/
const int NCHARCODES = 128;

/*****************************************************************************/
// GlyphFactory
class GlyphFactory {
public:
    GlyphFactory();
    virtual ~GlyphFactory();
/*
*/
    virtual Character* CreateCharacter(char);
    virtual Row* CreateRow();
    virtual Column* CreateColumn();
    // ...
private:
    Character* _character[NCHARCODES];
};
/*
*/
GlyphFactory::GlyphFactory () {
	output("GlyphFactory::GlyphFactory () \n");
    for (int i = 0; i < NCHARCODES; ++i) {
         _character[i] = 0;
    }
}

GlyphFactory::~GlyphFactory(){
	output("GlyphFactory::~GlyphFactory()\n");
}
/*
*/
Character* GlyphFactory::CreateCharacter (char c) {
	output("Character* GlyphFactory::CreateCharacter (char c) \n");
    if (!_character[c]) {
        _character[c] = new Character(c);
    }

    return _character[c];
}
/*
*/
Row* GlyphFactory::CreateRow () {
	output("Row* GlyphFactory::CreateRow () \n");
    return new Row;
}
/*
*/
Column* GlyphFactory::CreateColumn () {
	output("Column* GlyphFactory::CreateColumn () \n");
    return new Column;
}

void dummy2(){
	output("void dummy2()\n");

	GlyphFactory gf;
	Character* a = gf.CreateCharacter('a');
	Column* cl = gf.CreateColumn();
	Row * row = gf.CreateRow();


}
/*
*/

void main(){
	output("void main()\n");
	dummy();
	dummy2();
	pause();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -