---
title: "Jexi设计 (6) View设计"
author: "mmdev"
date: "2004-07-27T19:25:00Z"
tags: []
slug: "jexi-6-view"
draft: false
lastmod: "2004-07-27T19:25:00Z"
id: 1006712
url: "https://mmdev.iteye.com/blog/1006712"
---

以下讨论View的设计。

Document是抽象的文档类，它包含了所有的数据和如何显示的信息，我们已经通过Page，Paragraph，Row等等抽象出来了。

Frame代表窗体对象，负责构造出窗体，菜单栏，工具栏等等，它可以接受用户的命令，然后将命令传递给Document。

View代表视图对象，负责绘制文档数据，它在update()方法中绘制可视区域。

除了绘制文档数据，视图还可能要绘制滚动条，标尺等等。Decorator模式终于派上用场了，抽象出View接口，使用Decorator实现的继承体系如下：

![](/images/blog_csdn_net/asklxf/16543/o_view_design.gif)

先定义View接口：

> public interface View {  
> ??? void dispose();  
> ??? void update();  
> ??? void setActualSize(int width, int height);  
> ??? int getActualWidth();  
> ??? int getActualHeight();  
> ??? int getFullWidth();  
> ??? int getFullHeight();  
> ??? int getLeftPosition();  
> ??? int getTopPosition();  
> ??? void ensureCaretVisible();  
> }

再写抽象类ViewDecorator：

> public abstract class ViewDecorator implements View {  
> ??? protected View component = null;  
> ??? public ViewDecorator(View component) {  
> ??????? this.component = component;  
> ??? }  
> }

为了简化，我们只实现一个ScrollViewDecorator，RulerViewDecorator就不实现了。如果哪位有兴趣可以实现RulerViewDecorator，然后添加到体系中即可。

现在关注TextView，因为TextView需要将文档数据的可视部分显示出来，因此必须计算TextView的总大小（TextView的实际大小由上层控件决定，用户调整窗口大小时可能改变）。计算方法如下：

![](/images/blog_csdn_net/asklxf/16543/o_view_calc.gif)

红色部分就是TextView的总大小。