AlexLecture_TemplateMethod

AlexLecture_TemplateMethod - Template Method Document...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Template Method Document editor Allows user to view and or edit documents of various types Different kinds of documents: Plain text vs. rich text (with embedded fonts) Shared documents vs. non-shared documents Some users may edit, others may only browse Need to protect access when multiple users are editing Problem: Procedure to open/manipulate a document varies among different document types 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Example: Document class class Document { public: bool Open( const string& ); void Close(); void ReadLine( unsigned ); void AppendLine( unsigned, const string& ); void ReplaceLine( unsigned, const string& ); void View( unsigned, unsigned, Display* ); ... protected: FILE* f; }; 3 bool Document::Open( const string& name ) { if((f = fopen(name, “r+”))==0) return false; return true; } void Document::Close() { fclose(f); } Example: Adding rich text class Document { ... protected: FILE* f; vector<Font*> fonts; vector<Font > }; bool Document::Open( const string& name ) { if((f = fopen(name, “r+”))==0) return false; if (f && isRichTextFile(name)) { string fontFileName( name + “Fonts” ); FILE* fontfile; 4 if((fontfile=fopen(fontFileName,. ..))==0) return false; // load contents into fonts fclose(fontfile); return true; }
Background image of page 2
3 Example: Adding sharing support class Document { ... bool Open( const string& ); void Close(); ... protected: FILE* f; vector<Font*> fonts; FILE* lockFile; }; 5 Example: Document class bool Document::Open( const string& name ) { string lockName(“.lock” + name); if (isSharedFile(name) && testAndSet(lockFile,lockName)) return false; if((f = fopen(name, “r+”))==0) return false; if (f && isRichTextFile(name)) { string fontFileName( name + “Fonts” ); FILE* fontfile; if((fontfile=fopen(fontFileName,. ..))==0) return false; // load contents into fonts fclose(fontfile); } } 6 void Document::Close() { fclose(f); if (isSharedFile(name)) { unlink(lockFile); } return true; }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Problems with this solution Code quickly getting very complex Code for open has lots of case logic if (isSharedFile(name)), if (isRichTextFile(name)) Cases not even localized lock file created at beginning of open method must be deleted at end of close method Document class becoming a kitchen sink with everything needed for any kind of document 7 Question Can you think of a way to redesign this class to tame this escalating complexity escalating complexity?
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

AlexLecture_TemplateMethod - Template Method Document...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online