Skip to content

Commit

Permalink
* Added initial version of Flyweight, which compiles but is not compl…
Browse files Browse the repository at this point in the history
…ete yet.
  • Loading branch information
BartVandewoestyne committed Dec 13, 2012
1 parent be1c991 commit c50a0f5
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 2.6)

project(design_patterns)
project(design_patterns CXX)

set(CMAKE_CXX_FLAGS "-g -Wall -Wextra")
#set(CMAKE_CXX_FLAGS "-g")
Expand Down
1 change: 1 addition & 0 deletions Structural_Patterns/CMakeLists.txt
Expand Up @@ -3,3 +3,4 @@ add_subdirectory(Bridge)
add_subdirectory(Composite)
add_subdirectory(Decorator)
add_subdirectory(Facade)
add_subdirectory(Flyweight)
13 changes: 13 additions & 0 deletions Structural_Patterns/Flyweight/CMakeLists.txt
@@ -0,0 +1,13 @@
#include_directories(../../Foundation_Classes)
#include_directories(../../Behavioral_Patterns/Iterator)

set(flyweight_SRCS
Character.cpp
Font.cpp
Glyph.cpp
GlyphContext.cpp
GlyphFactory.cpp)

add_executable(flyweight1 flyweight1.cpp)
add_library(flyweight SHARED ${flyweight_SRCS})
target_link_libraries(flyweight1 flyweight)
8 changes: 8 additions & 0 deletions Structural_Patterns/Flyweight/Character.cpp
@@ -0,0 +1,8 @@
#include "Character.h"

Character::Character(char charcode)
: _charcode(charcode)
{}

void Character::Draw(Window*, GlyphContext&)
{}
18 changes: 18 additions & 0 deletions Structural_Patterns/Flyweight/Character.h
@@ -0,0 +1,18 @@
#ifndef CHARACTER_H
#define CHARACTER_H

#include "Glyph.h"

class Window;
class GlyphContext;

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

virtual void Draw(Window*, GlyphContext&);
private:
char _charcode;
};

#endif /* CHARACTER_H */
7 changes: 7 additions & 0 deletions Structural_Patterns/Flyweight/Column.h
@@ -0,0 +1,7 @@
#ifndef COLUMN_H
#define COLUMN_H

class Column {
};

#endif /* COLUMN_H */
5 changes: 5 additions & 0 deletions Structural_Patterns/Flyweight/Font.cpp
@@ -0,0 +1,5 @@
#include "Font.h"

Font::Font(char* name)
: _name(name)
{}
11 changes: 11 additions & 0 deletions Structural_Patterns/Flyweight/Font.h
@@ -0,0 +1,11 @@
#ifndef FONT_H
#define FONT_H

class Font {
public:
Font(char*);
private:
char* _name;
};

#endif /* FONT_H */
61 changes: 61 additions & 0 deletions Structural_Patterns/Flyweight/Glyph.cpp
@@ -0,0 +1,61 @@
#include "Glyph.h"

#include <iostream>
using namespace std;

Glyph::Glyph()
{
cout << "Glyph::Glyph()" << endl;
}

Glyph::~Glyph()
{
cout << "Glyph::~Glyph()" << endl;
}

void Glyph::Draw(Window*, GlyphContext&)
{
cout << "Glyph::Draw(Window*, GlyphContext&)" << endl;
}

void Glyph::SetFont(Font*, GlyphContext&)
{
cout << "Glyph::SetFont(Font*, GlyphContext&)" << endl;
}

Font* Glyph::GetFont(GlyphContext&)
{
cout << "Glyph::GetFont(GlyphContext&)" << endl;
return 0;
}

void Glyph::First(GlyphContext&){
cout << "Glyph::First(GlyphContext&)" << endl;
}

void Glyph::Next(GlyphContext&)
{
cout << "Glyph::Next(GlyphContext&)" << endl;
}

bool Glyph::IsDone(GlyphContext&)
{
cout << "Glyph::IsDone(GlyphContext&)" << endl;
return 0;
}

Glyph* Glyph::Current(GlyphContext&)
{
cout << "Glyph* Glyph::Current(GlyphContext&)" << endl;
return 0;
}

void Glyph::Insert(Glyph*, GlyphContext&)
{
cout << "void Glyph::Insert(Glyph*, GlyphContext&)" << endl;
}

void Glyph::Remove(GlyphContext&)
{
cout << "void Glyph::Remove(GlyphContext&)" << endl;
}
28 changes: 28 additions & 0 deletions Structural_Patterns/Flyweight/Glyph.h
@@ -0,0 +1,28 @@
#ifndef GLYPH_H
#define GLYPH_H

class GlyphContext;
class Font;
class Window;

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();
};

#endif /* GLYPH_H */
26 changes: 26 additions & 0 deletions Structural_Patterns/Flyweight/GlyphContext.cpp
@@ -0,0 +1,26 @@
// TODO: implement these.
#include "GlyphContext.h"

GlyphContext::GlyphContext()
{}

GlyphContext::~GlyphContext()
{}

void GlyphContext::Next(int step)
{}

void GlyphContext::Insert(int quantity1)
{}

Font* GlyphContext::GetFont()
{
// TODO
return 0;
}

void GlyphContext::SetFont(Font*, int span)
{}

// int _index;
// BTree* _fonts;
22 changes: 22 additions & 0 deletions Structural_Patterns/Flyweight/GlyphContext.h
@@ -0,0 +1,22 @@
#ifndef GLYPH_CONTEXT_H
#define GLYPH_CONTEXT_H

class BTree;
class Font;

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;
};

#endif /* GLYPH_CONTEXT_H */
33 changes: 33 additions & 0 deletions Structural_Patterns/Flyweight/GlyphFactory.cpp
@@ -0,0 +1,33 @@
#include "GlyphFactory.h"
#include "Character.h"
#include "Row.h"
#include "Column.h"

GlyphFactory::GlyphFactory()
{
for (int i = 0; i < NCHARCODES; ++i) {
_character[i] = 0;
}
}

GlyphFactory:: ~GlyphFactory()
{
delete[] _character;
}

Character* GlyphFactory::CreateCharacter(char c)
{
if (!_character[c]) {
_character[c] = new Character(c);
}

return _character[c];
}

Row* GlyphFactory::CreateRow() {
return new Row;
}

Column* GlyphFactory::CreateColumn() {
return new Column;
}
23 changes: 23 additions & 0 deletions Structural_Patterns/Flyweight/GlyphFactory.h
@@ -0,0 +1,23 @@
#ifndef GLYPH_FACTORY_H
#define GLYPH_FACTORY_H

class Character;
class Column;
class Row;

const int NCHARCODES = 128;

class GlyphFactory {
public:
GlyphFactory();
virtual ~GlyphFactory();

virtual Character* CreateCharacter(char);
virtual Row* CreateRow();
virtual Column* CreateColumn();
// ...
private:
Character* _character[NCHARCODES];
};

#endif /* GLYPH_FACTORY_H */
7 changes: 7 additions & 0 deletions Structural_Patterns/Flyweight/Row.h
@@ -0,0 +1,7 @@
#ifndef ROW_H
#define ROW_H

class Row {
};

#endif /* ROW_H */
15 changes: 15 additions & 0 deletions Structural_Patterns/Flyweight/flyweight1.cpp
@@ -0,0 +1,15 @@
#include "Font.h"
#include "GlyphContext.h"

int main()
{
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);
}

0 comments on commit c50a0f5

Please sign in to comment.