lexi设计案例分析.ppt_第1页
lexi设计案例分析.ppt_第2页
lexi设计案例分析.ppt_第3页
lexi设计案例分析.ppt_第4页
lexi设计案例分析.ppt_第5页
已阅读5页,还剩71页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1,第2章 实例研究:Lexi 文档编辑器,A WYSIWYG document editor. Mix text and graphics freely in various formatting styles. The usual Pull-down menus Scroll bars Page icons for jumping around the document. 通过本实例设计,学习设计模式的实际应用,2,2.1 设计问题,Lexi设计的7个问题 1 文档结构:对文档内部表示的选择几乎影响Lexi设计的每个方面。 2 格式化 3 修饰用户界面 4 支持多种视感标准 5 支持多种窗

2、口系统 6 用户操作 7 拼写检查 上述每个问题都有一组相关联的目标集合和限制条件集合。,3,2.2 文档结构,目标 保持文档的物理结构。即将文本和图形安排到行、列和表等。 可视化生成和显示文档。 根据显示位置来映射文档内部表示的元素。 限制条件 应该一致地对待文本和图形。 应该一致地对待简单元素和复合元素。 但文本分析依赖于被分析对象的类型。,4,解决方案:递归组合,递归组合:Building more complex elements out of simpler ones. 行列(段落)页 (P24 第2段第2行)第5行第2列的第10个元素 The tenth element in li

3、ne five of column two, 隐含: Each object type needs a corresponding class All must have compatible interfaces (inheritance),图2 包含正文和图形的递归组合,图3 递归组合的对象结构,5,Glyph(图元类),Base class for composable graphical objects An Abstract class for all objects that can appear in a document. Both primitive and composed

4、.,子类: Character, Image, Space, Row, Column,6,图元类层次,Note the inherent recursion in this hierarchy i.e., a Row is a Glyph public abstract Rect getBounds(); / hit detection public abstract boolean intersects(Point); / structure public abstract void insert(Glyph g, int i); public abstract void remove(Gl

5、yph g); public abstract Glyph child(int i); public abstract Glyph parent(); ,8,COMPOSITE 模式 object structural,意图 treat individual objects drawBorder(); ,Decorator,21,Transparent Enclosure,single-child composition compatible interfaces Enclosure will delegate operations to single child, but can add s

6、tate augment by doing work before or after delegating to the child.,22,DECORATOR 模式 object structural,意图 augment One object with new responsibilities 适用性 when extension by subclassing is impractical for responsibilities that can be withdrawn,Structure,23,DECORATOR模式 (contd) object structural,效果 resp

7、onsibilities can be added/removed at run-time avoids subclass explosion recursive nesting allows multiple responsibilities 实现 interface conformance use a lightweight, abstract base class for Decorator,24,2.5 支持多种视感标准,Want the application to be portable across diverse user interface libraries. Every

8、user interface element will be a Glyph. Some will delegate to appropriate platform-specific operations.,25,Multiple Look useScrollbar *sb = factory-createScrollbar(); where factory is an instance of MotifFactory this begs the question of who created the factory!,27,Factory Interface,defines “manufac

9、turing interface” subclasses produce specific products subclass instance chosen at run-time,/ This class is essentially a Java interface class Factory public: Scrollbar *createScrollbar() = 0; Menu *createMenu() = 0; . ;,28,Object Factories,Usual method: ScrollBar sb = new MotifScrollBar(); Factory

10、method: ScrollBar sb = guiFactory.createScrollBar();,29,Product Objects,The output of a factory is a product.,abstract,concrete,Abstract Factory,30,Building the Factory,If known at compile time (e.g., Lexi v1.0 only Motif implemented). GUIFactory guiFactory = new MotifFactory(); Set at startup (Lexi

11、 v2.0) String LandF = appProps.getProperty(LandF); GUIFactory guiFactory; if( LandF.equals(Motif) ) guiFactory = new MotifFactory(); . Changeable by a menu command (Lexi v3.0) re-initialize guiFactory re-build the UI,Singleton,31,ABSTRACT FACTORY 模式 object creational,意图 create families of related ob

12、jects without specifying class names 适用性 when clients cannot anticipate groups of classes to instantiate,Structure,32,ABSTRACT FACTORY模式 (contd) object creational,效果 flexibility: removes type dependencies from clients abstraction: hides products composition hard to extend factory interface to create

13、 new products 实现 parameterization as a way of controlling interface size configuration with Prototypes, i.e., determines who creates the factories,33,2.6 支持多种窗口系统,目标: make composition appear in a window support multiple window systems 限制条件: minimize window system dependencies in application / window

14、-management void raise(); . void drawLine(.); / device-independent void drawText(.); / graphics interface . ;,Window Interface,36,Window 实现,Defined interface Lexi deals with, but where does the real windowing library come into it? Could define alternate Window classes . public class Window public vo

15、id drawRect(Coord x0,y0,x1,y1) imp.drawRect(x0,y0,x1,y1); . public class XWindowImp extends WindowImp public void drawRect(Coord x0,y0,x1,y1) . XDrawRectangle(display, windowId, graphics, x,y,w,h); ,38,配置 imp,public abstract class WindowSystemFactory public abstract WindowImp createWindowImp(); publ

16、ic abstract ColorImp createColorImp(); . public class XWindowSystemFactory extends WindowSystemFactory public WIndowImp createWindowImp() return new XWindowImp(); . public class Window Window() imp = windowSystemFactory.createWindowImp(); . ,Abstract Factory,well-known object,39,对象结构,Note :the decou

17、pling between the logical structure of the contents in a window from the physical rendering of the contents in the window,40,BRIDGE 模式 object structural,意图 separate a (logical) abstraction interface from its (physical) implementation(s) 适用性 when interface ,void PasteCommand:execute () / do the paste

18、 void CopyCommand:execute () / do the copy,47,Invoking Commands,When an interactive Glyph is tickled, it calls the Command object with which it has been initialized.,Command,48,Undo/Redo,Add an unexecute() method to Command Reverses the effects of a preceding execute() operation using whatever undo

19、information execute() stored into the Command object. Add a isUndoable() and a hadnoEffect() method Maintain Command history:,49,COMMAND模式 object behavioral,意图 encapsulate the request for a service 适用性 to parameterize objects with an action to perform for multilevel undo/redo,Structure,50,COMMAND模式(

20、contd) object behavioral,效果 abstracts executor of a service supports arbitrary-level undo-redo composition yields macro-commands might result in lots of trivial command subclasses,51,2.8 拼写检查和断字处理,Textual analysis checking for misspellings introducing hyphenation points where needed for good formatt

21、ing. Want to support multiple algorithms. Want to make it easy to add new algorithms. Want to make it easy to add new types of textual analysis word count grammar Legibility(易读性) Wish to de-couple textual analysis from the Glyph classes.,52,2.8 拼写检查和断字处理,目标: analyze text for spelling errors introduc

22、e potential hyphenation(断字) sites 限制条件: support multiple algorithms dont tightly couple algorithms with document structure,53,Accessing Scattered Information,Need to access the text letter-by-letter. Our design has text scattered all over the Glyph hierarchy. Different Glyphs have different data str

23、uctures for storing their children (lists, trees, arrays, ). Sometimes need alternate access patterns: spell check: forward search back: backwards evaluating equations: inorder tree traversal,54,Encapsulating Access !g.done(); g-next() Glyph current = g-getCurrent(); Problems: cant support new trave

24、rsals without extending enum and modifying all parent Glyph types. Cant re-use code to traverse other object structures (e.g., Command history).,55,解决方案: 封装遍历,Iterator encapsulates a traversal algorithm without exposing representation details to callers uses Glyphs child enumeration operation This i

25、s an example of a “preorder iterator”,56,Iterator 层次,Iterator,57,Using Iterators,Glyph* g; Iterator* i = g-CreateIterator(); for (i-First(); !i-IsDone(); i-Next() Glyph* child = i-CurrentItem(); / do something with current child ,58,Initializing Iterators,Iterator* Row:CreateIterator () return new L

26、istIterator(_children); ,59,Implementing a Complex Iterator,void PreorderIterator:First () Iterator* i = _root-CreateIterator(); if (i) i-First(); _iterators.RemoveAll(); _iterators.Push(i); Glyph* PreorderIterator:CurrentItem () const return _iterators.Size() 0 ? _iterators.Top()-CurrentItem() : 0;

27、 ,60,Implementing a Complex Iterator (contd),void PreorderIterator:Next () Iterator* i = _iterators.Top()-CurrentItem()-CreateIterator(); i-First(); _iterators.Push(i); while ( _iterators.Size() 0 ,61,ITERATOR模式 object behavioral,意图 access elements of a container without exposing its representation

28、适用性 require multiple traversal algorithms over a container require a uniform traversal interface over different containers when container classes for (int i = 0; i :iterator i (args.begin (); i != args.end (); i+) cout *i; cout endl; return 0; ,Iterators are used heavily in the C+ Standard Template

29、Library (STL),The same iterator pattern can be applied to any STL container!,64,访问动作,Now that we can traverse, we need to add actions while traversing that have state spelling, hyphenation, Could augment the Iterator classes but that would reduce their reusability Could augment the Glyph classes but

30、 will need to change Glyph classes for each new analysis Will need to encapsulate the analysis in a separate object,65,Visitor,defines action(s) at each step of traversal avoids wiring action(s) into Glyphs iterator calls glyphs accept(Visitor) at each node accept() calls back on visitor (a form of

31、“static polymorphism” based on method overloading by type),void Character:accept (Visitor ,66,Actions in Iterators,Iterator will carry the analysis object along with it as it iterates. The analyzer will accumulate state. e.g., characters for a spell check,67,Avoiding Downcasts,How can the analysis o

32、bject distinguish different kinds of Glyphs without resorting to switch statements and downcasts. e.g., avoid: public class SpellingChecker extends public void check(Glyph g) if( g instanceof CharacterGlyph ) CharacterGlyph cg = (CharacterGlyph)g; / analyze the character else if( g instanceof RowGly

33、ph ) rowGlyph rg = (RowGlyph)g; / prepare to analyze the child glyphs else ,68,Accepting Visitors,public abstract class Glyph public abstract void accept(Visitor v); public class CharacterGlyph extends Glyph public void accept(Visitor v) v.visitCharacterGlyph(this); ,69,Visitor /* do nothing */ publ

34、ic class SpellingVisitor extends Visitor public void visitCharacterGlyph(CharacterGlyph cg) ,Visitor,70,SpellingVisitor,public class SpellingVisitor extends Visitor private Vector misspellings = new Vector(); private String currentWord = “”; public void visitCharacterGlyph(CharacterGlyph cg) char c

35、= cg-getChar(); if( isalpha(c) ) currentWord += c; else if( isMispelled(currentWord) ) / add misspelling to list misspelling.addElement(currentWord); currentWord = “”; public Vector getMisspellings return misspellings; ,71,Using SpellingVisitor,PreorderIterator i = new PreorderIterator(); i.setVisitor(new SpellingVisitor(); i.visitAll(rootGlyph); Vector misspellings = (SpellingVisitor)i.getVisistor().getMisspellings(); public class Iterator private Visitor v; public void visitAll(Gly

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论