From c50a0f5d8f350d70f7eff0759db7e1d1fe3ab05d Mon Sep 17 00:00:00 2001 From: Bart Vandewoestyne Date: Thu, 13 Dec 2012 15:53:08 +0100 Subject: [PATCH] * Added initial version of Flyweight, which compiles but is not complete yet. --- CMakeLists.txt | 2 +- Structural_Patterns/CMakeLists.txt | 1 + Structural_Patterns/Flyweight/CMakeLists.txt | 13 ++++ Structural_Patterns/Flyweight/Character.cpp | 8 +++ Structural_Patterns/Flyweight/Character.h | 18 ++++++ Structural_Patterns/Flyweight/Column.h | 7 +++ Structural_Patterns/Flyweight/Font.cpp | 5 ++ Structural_Patterns/Flyweight/Font.h | 11 ++++ Structural_Patterns/Flyweight/Glyph.cpp | 61 +++++++++++++++++++ Structural_Patterns/Flyweight/Glyph.h | 28 +++++++++ .../Flyweight/GlyphContext.cpp | 26 ++++++++ Structural_Patterns/Flyweight/GlyphContext.h | 22 +++++++ .../Flyweight/GlyphFactory.cpp | 33 ++++++++++ Structural_Patterns/Flyweight/GlyphFactory.h | 23 +++++++ Structural_Patterns/Flyweight/Row.h | 7 +++ Structural_Patterns/Flyweight/flyweight1.cpp | 15 +++++ 16 files changed, 279 insertions(+), 1 deletion(-) create mode 100644 Structural_Patterns/Flyweight/CMakeLists.txt create mode 100644 Structural_Patterns/Flyweight/Character.cpp create mode 100644 Structural_Patterns/Flyweight/Character.h create mode 100644 Structural_Patterns/Flyweight/Column.h create mode 100644 Structural_Patterns/Flyweight/Font.cpp create mode 100644 Structural_Patterns/Flyweight/Font.h create mode 100644 Structural_Patterns/Flyweight/Glyph.cpp create mode 100644 Structural_Patterns/Flyweight/Glyph.h create mode 100644 Structural_Patterns/Flyweight/GlyphContext.cpp create mode 100644 Structural_Patterns/Flyweight/GlyphContext.h create mode 100644 Structural_Patterns/Flyweight/GlyphFactory.cpp create mode 100644 Structural_Patterns/Flyweight/GlyphFactory.h create mode 100644 Structural_Patterns/Flyweight/Row.h create mode 100644 Structural_Patterns/Flyweight/flyweight1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ee098ee..ccf48cb 100644 --- a/CMakeLists.txt +++ b/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") diff --git a/Structural_Patterns/CMakeLists.txt b/Structural_Patterns/CMakeLists.txt index 1caa63b..152f2fc 100644 --- a/Structural_Patterns/CMakeLists.txt +++ b/Structural_Patterns/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory(Bridge) add_subdirectory(Composite) add_subdirectory(Decorator) add_subdirectory(Facade) +add_subdirectory(Flyweight) diff --git a/Structural_Patterns/Flyweight/CMakeLists.txt b/Structural_Patterns/Flyweight/CMakeLists.txt new file mode 100644 index 0000000..a886f34 --- /dev/null +++ b/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) diff --git a/Structural_Patterns/Flyweight/Character.cpp b/Structural_Patterns/Flyweight/Character.cpp new file mode 100644 index 0000000..52bb6dd --- /dev/null +++ b/Structural_Patterns/Flyweight/Character.cpp @@ -0,0 +1,8 @@ +#include "Character.h" + +Character::Character(char charcode) + : _charcode(charcode) +{} + +void Character::Draw(Window*, GlyphContext&) +{} diff --git a/Structural_Patterns/Flyweight/Character.h b/Structural_Patterns/Flyweight/Character.h new file mode 100644 index 0000000..e644272 --- /dev/null +++ b/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 */ diff --git a/Structural_Patterns/Flyweight/Column.h b/Structural_Patterns/Flyweight/Column.h new file mode 100644 index 0000000..5fbe8b6 --- /dev/null +++ b/Structural_Patterns/Flyweight/Column.h @@ -0,0 +1,7 @@ +#ifndef COLUMN_H +#define COLUMN_H + +class Column { +}; + +#endif /* COLUMN_H */ diff --git a/Structural_Patterns/Flyweight/Font.cpp b/Structural_Patterns/Flyweight/Font.cpp new file mode 100644 index 0000000..a53e12b --- /dev/null +++ b/Structural_Patterns/Flyweight/Font.cpp @@ -0,0 +1,5 @@ +#include "Font.h" + +Font::Font(char* name) + : _name(name) +{} diff --git a/Structural_Patterns/Flyweight/Font.h b/Structural_Patterns/Flyweight/Font.h new file mode 100644 index 0000000..4438f20 --- /dev/null +++ b/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 */ diff --git a/Structural_Patterns/Flyweight/Glyph.cpp b/Structural_Patterns/Flyweight/Glyph.cpp new file mode 100644 index 0000000..713af4f --- /dev/null +++ b/Structural_Patterns/Flyweight/Glyph.cpp @@ -0,0 +1,61 @@ +#include "Glyph.h" + +#include +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; +} diff --git a/Structural_Patterns/Flyweight/Glyph.h b/Structural_Patterns/Flyweight/Glyph.h new file mode 100644 index 0000000..7c5bf15 --- /dev/null +++ b/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 */ diff --git a/Structural_Patterns/Flyweight/GlyphContext.cpp b/Structural_Patterns/Flyweight/GlyphContext.cpp new file mode 100644 index 0000000..fdf63ed --- /dev/null +++ b/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; diff --git a/Structural_Patterns/Flyweight/GlyphContext.h b/Structural_Patterns/Flyweight/GlyphContext.h new file mode 100644 index 0000000..c54de67 --- /dev/null +++ b/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 */ diff --git a/Structural_Patterns/Flyweight/GlyphFactory.cpp b/Structural_Patterns/Flyweight/GlyphFactory.cpp new file mode 100644 index 0000000..54d1fbc --- /dev/null +++ b/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; +} diff --git a/Structural_Patterns/Flyweight/GlyphFactory.h b/Structural_Patterns/Flyweight/GlyphFactory.h new file mode 100644 index 0000000..cdb1433 --- /dev/null +++ b/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 */ diff --git a/Structural_Patterns/Flyweight/Row.h b/Structural_Patterns/Flyweight/Row.h new file mode 100644 index 0000000..20345e7 --- /dev/null +++ b/Structural_Patterns/Flyweight/Row.h @@ -0,0 +1,7 @@ +#ifndef ROW_H +#define ROW_H + +class Row { +}; + +#endif /* ROW_H */ diff --git a/Structural_Patterns/Flyweight/flyweight1.cpp b/Structural_Patterns/Flyweight/flyweight1.cpp new file mode 100644 index 0000000..11c85c5 --- /dev/null +++ b/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); +}