《lexi设计案例分析.ppt》由会员分享,可在线阅读,更多相关《lexi设计案例分析.ppt(76页珍藏版)》请在优知文库上搜索。
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. 通过本实例设计,学习设计模式的实际应用22.1 设计问题 Lexi设计的7个问题 1 文档结构:对文档内部表示的选择几乎影响Lexi设计的每个方面。 2 格式化 3 修饰用户界面 4 支持多种视感标准 5 支持多种窗口系统
2、 6 用户操作 7 拼写检查 上述每个问题都有一组相关联的目标集合和限制条件集合。32.2 文档结构 目标 保持文档的物理结构。即将文本和图形安排到行、列和表等。 可视化生成和显示文档。 根据显示位置来映射文档内部表示的元素。 限制条件 应该一致地对待文本和图形。 应该一致地对待简单元素和复合元素。 但文本分析依赖于被分析对象的类型。4解决方案:递归组合递归组合:Building more complex elements out of simpler ones. 行列(段落)页 (P24 第2段第2行)第5行第2列的第10个元素 The tenth element in line five
3、of column two,隐含: Each object type needs a corresponding class All must have compatible interfaces (inheritance)图2 包含正文和图形的递归组合图3 递归组合的对象结构5Glyph (图元类)Base class for composable graphical objectsAn Abstract class for all objects that can appear in a document. Both primitive and composed.void insert(G
4、lyph)void remove(Glyph)Glyph child(int)Glyph parent()管理子图元的接口boolean intersects(Coord, Coord)判断一个指定的点是否与图元相交void draw(Window*)Void Bounds(Rect)在窗口上表示自己返回图元占用的矩形面积操作任务基本接口:子类: Character, Image, Space, Row, Column 6图元类层次Note the inherent recursion in this hierarchywi.e., a Row is a Glyph & a Row also
5、has Glyphs!7Glyph Interface and responsibilitiesGlyphs know how to draw themselvesGlyphs know what space they occupyGlyphs know their children and parentspublic abstract class Glyph / appearance public abstract void draw(Window w); public abstract Rect getBounds(); / hit detection public abstract bo
6、olean intersects(Point); / structure public abstract void insert(Glyph g, int i); public abstract void remove(Glyph g); public abstract Glyph child(int i); public abstract Glyph parent();8COMPOSITE 模式 object structural意图treat individual objects & multiple, recursively-composed objects uniformly适用obj
7、ects must be composed recursively,and no distinction between individual & composed elements,and objects in structure can be treated uniformlyStructure9COMPOSITE 模式(contd) object structural效果+uniformity: treat components the same regardless of complexity+extensibility: new Component subclasses work w
8、herever old ones do实现do Components know their parents?保持从子部件到父部件的引用能简化组合结构的遍历和管理uniform interface for both leaves & composites?最大化Component接口dont allocate storage for children in Component base classresponsibility for deleting children由Composite负责删除其子节点102.3 格式化 格式化:将一个图元集合分解为若干行 目标:自动换行 Breaking up
9、 a document into lines. Many different algorithms trade off quality for speed Complex algorithms 限制条件 Want to keep the formatting algorithm well-encapsulated. independent of the document structure can add formatting algorithm without modifying Glyphs can add Glyphs without modifying the formatting a
10、lgorithm. Want to make it dynamically changeable.11Composition & CompositorCompositor base class abstracts linebreaking algorithm subclasses for specialized algorithms, e.g., SimpleCompositor, TeXCompositor 接口接口 格式化内容:格式化内容:void SetComposition(Composition*) 格式化:格式化:virtual void Compose()()Compositio
11、n composite glyph supplied a compositor & leaf glyphs creates row-column structure as directed by compositor12Composition & Compositor一个未格式化的Composition对象只包含组成文档基本内容的可见图元,它并不包含像行和列这样决定文档物理结构的图元。 Composite对象只在刚被创建并以待格式化的图元进行初始化后的状态当Composition对象需要格式化时,调用它的Compositor的Compose操作。Compositor依次遍历Compositio
12、n的各个图元,根据分行算法插入新的行和列图元。Generated in accordance with compositor strategies & do not affect contents of leaf glyphs13Compositor & CompositionCompositor class will encapsulate a formatting algorithm.14Compositor & Composition 分行算法封装 能增加新的Compositor子类而不触及Glyph类 可在运行时刻改变分行算法 在Composition接口中增加一个SetComposi
13、tor操作15STRATEGY模式 object behavioral意图define a family of algorithms, encapsulate each one, & make them interchangeable to let clients & algorithms vary independently适用性when an object should be configurable with one of many algorithms,and all algorithms can be encapsulated,and one interface covers all
14、 encapsulations 结构16STRATEGY模式 (contd) object behavioral效果+greater flexibility, reuse+can change algorithms dynamicallystrategy creation & communication overheadinflexible Strategy interfacesemantic incompatibility of multiple strategies used together实现exchanging information between a Strategy & its
15、 contextstatic strategy selection via templates172.4 修饰用户界面 Wish to add visible borders and scroll-bars around pages. Inheritance is one way to do it. leads to class proliferation BorderedComposition, ScrollableComposition, BorderedScrollableComposition inflexible at run-time Will have classes Borde
16、r Scroller They will be Glyphs they are visible clients shouldnt care if a page has a border or not They will be composed. but in what order?182.4 修饰用户界面目标: add a frame around text composition add scrolling capability限制条件: embellishments should be reusable without subclassing, i.e., so they can be added dynamically at runtime should go unnoticed by clients19解决方案: “Transparent” Enclosure(透明围栏)Monoglyph:起修饰作用的图元的抽象类:起修饰作用的图元的抽象类 base class for glyphs having one child operations on MonoGlyph pass t