Design Patterns in Smalltalk MVC在Smalltalk的MVC设计模式
The Model/View/Controller (MVC) triad ofclasses [KP88] is used to build user interfaces in Smalltalk-80. Looking at thedesign patterns inside MVC should help
you see what we mean by the term"pattern."
模型/视图/控制器(MVC)类[kp88 ] 是用来在Smalltalk-80构建用户界面。关注在MVC 中的设计模式,会帮助我们理解模式这一术语。
MVC consists of three kinds of objects. TheModel is the application object, the View is its screen presentation, and theController defines the way the user interface reacts to user input. Before MVC,user interface designs tended to lump these objects together. MVC decouples themto increase flexibility and reuse.
MVC有三种对象。模型是应用程序对象,视图是屏幕介绍,控制器其定义了用户在输入之前界面做出反应的方法。用户界面通常把它们结合在一起。MVC减弱它们的结合来增加灵活性和重用性。
MVC decouples views and models byestablishing asubscribe/notify protocol between them. A view must ensure that its appearance reflects the state of the model.Whenever the model's data changes, themodel notifies views that depend on it. In response, each view gets an opportunityto update itself. This approach lets you attach multiple views to a model to providedifferent presentations. You can also create new views for a model withoutrewriting it.
通过视图和模型之间的协议,MVC减弱了它们。视图必须可靠,因为它表达了模型的状态。无论什么时候模型数据改变,模型所确定的视图也依赖它。为此,视图得到了更新它的机会。这种方法使多功能的视图和模型提供的介绍联系在一起了,你也可以创建一个新的视图而不需要重写它。
The following diagram shows a model andthree views. (We've left out the controllers for simplicity.) The model contains somedata values, and the views defining a spreadsheet, histogram, and pie chartdisplay these data in various ways. The model communicates with its views when itsvalues change, and the views communicate
with the model to access these values.
接下来的图表展示了模型和三个视图的关系。(没有添加控制器只是为了简单的说明情况)模型包含了一些数值,视图定义了直方图,电子表格,饼状图等不同的方法来显示这些数据。模型和视图通信在数据改变的时候。视图和模型通信在模型获得这些数据的时候。
Taken at face value, this example reflects a design that decouples views from models. But the design is applicable to amore general problem: decoupling objects so that changes to one can affect anynumber of others without requiring the changed object to know details of the others. Thismore general design is described by the Observer (page 326) design pattern.
从表面看,这个例子表明了设计中减弱了来自模型的视图。但是这个设计的使用有些普遍存在的问题:减弱对象会导致这种改变影响对象中其他的没有要求改变数据,以至于需要知道其他数据的详细。通过观察设计模式,更普遍的设计是描述。
Another feature of MVC is that views can be nested. For example, a control panel of buttons might be implemented as a complex view containing nested button views.The user interface for an object inspector can consist of nested views thatmay be reused in a debugger. MVC supportsnested views with the Composite View class,a subclass of View. CompositeView objects act just likeView objects; a composite view can be used wherever a view can beused, but it also contains and manages nested views.
MVX的另一个特点是视图可以被嵌套。例如,复杂的视图包含嵌套的视图,这样的控制面板按钮可能会被实现。对象检测员的用户界面可以由嵌套的视图组成,在调试的过程中可以被重用。通过综合的视图类和视图的子类,MVC支持嵌套的视图。综合视图对象的行为就像视图对象。视图的子类可以被用在任何需要用视图的地方,它也含有和管理嵌套视图。
Again, we could think of this as a designthat lets us treat a composite view just like we treat one of its components.But thedesign is applicable to a more general problem, which occurs whenever we wantto group objects and treat the group like an individual object. This moregeneral design is described by the Composite (183) design pattern. It lets youcreate a class hierarchy in which some subclasses define primitive objects(e.g., Button) and other classes define composite objects (CompositeView) thatassemble the primitives into more complex objects
在我们可以考虑设计时,对待设计就像对待综合视图中的每一个视图构成要素那样.但是这个设计在应用时可能会有一些普遍比存在的问题。这些会发生在我们想要组对象或对待这个组对像一个独立的对象一样。更普遍的设计是通过综合设计模式来描述。在子类定义原始对象或其他类创建综合对象组成更复杂的对象的时候,它会让你创建类的层次。
MVC also lets you change the way a viewresponds to user input without changing its visual presentation. You might want tochange the way it responds to the keyboard, for example, or have it use a pop-up menuinstead of command keys. MVC encapsulates the response mechanism in a Controllerobject. There is a class hierarchy of controllers, making it easy to create a newcontroller as a variation on an existing one.
MVC也提供视图回应在用户输入没有改变的时候的视觉显示改变的方法。你可能想改变回应键盘输入额方法。例如,用弹出的菜单来代替命令键。MVC的控制器对象包含了所有的回应结构。有类的层次控制,使它更容易在现有的变化中创建新的控制结构。
A view uses an instance ofa Controller subclass toimplementa particular response Strategy to implement a different strategy, simply replace the instancewith a different kind of controller. It's evenpossible to change a view's controller at run-time to let the view change the wayit responds to user input. For example, a view can be disabled so that it doesn'taccept input simply by giving it a controller that ignores input events.
一个视图使用控制器实现特别的响应策略的实例,来实现用不同的方案,用不同的控制器做出简单回应的实例。改变视图控制器,这样做是可能的,在程序运行时,改变视图回应用户进行输入的方法。例如,视图可以不起效,通过一个忽略输入信息的控制器,让它不接受用户的输入。
The View-Controller relationship is anexample of the Strategy (349) design pattern. A Strategy is an object that represents analgorithm. It's useful when you want to replace the algorithm either staticallyor dynamically, when you have a lot of variants of the algorithm, or when thealgorithm has complex data structures that you want to encapsulate.
在349页的设计模式中,有视图控制关系的范例。一个方案是对象是一个算法。你想动态或静的代替这个算法时,当你有各种各样的算法时,或者是算法有复杂的数据结构,你想去囊括它时,它都是有晓得。。
MVC uses other design patterns, such asFactory Method (121) to specify the default controller class for a view and Decorator(196) to add scrolling to a view. But the main relationships in MVC are given bythe Observer, Composite, and Strategy design patterns.
MVC也用其他的设计模式,例如工厂模式为视图指定默认控制结构类和装饰添加滚动视图。但是最主要的关系通过观察,比较,分析,得出设计模式。