本文介绍了在Sketch示例AppKit应用程序中的类如何分为Models / Views / Controllers?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Sketch的示例AppKit应用程序(位于/ Developer / Examples / AppKit / Sketch)中的Models或Views的定义有些困惑。类SKTRectangle,SKTCircle等被认为是模型类,但他们有绘图代码。



我的印象是模型应该没有任何视图/绘图代码。





谢谢。

解决方案

Sketch的开发者/她的理由有点在ReadMe文件:






模型视图控制器设计 / p>

Sketch的Model层主要是SKTGraphic类及其子类。草图文档由SKTGraphics列表组成。 SKTGraphics主要是数据承载类。每个图形保存代表任何图形所需的所有信息。 SKTGraphic类定义了一组用于修改图形的原始方法,一些子类添加了它们自己的新原语。 SKTGraphic类还定义了一些修改图形的扩展方法,这些方法是用原语实现的。



SKTGraphic类定义了一组允许它的方法绘制自己。虽然这可能不是严格看来它应该是模型的一部分,请记住,我们正在建模是一个视觉对象的集合。即使SKTGraphic知道如何在视图中渲染自己,它也不是视图本身。






我不知道这是否是一个令人满意的答案。我的MVC的个人经验是,虽然分离模型,视图和控制器是一个好东西,在实践中,层之间的线条变得模糊。我认为设计的妥协经常是为了方便。



对于Sketch,特别是对于我来说,模型知道如何在视图中绘制自己是有意义的。使每个SKTGraphic子类知道如何绘制自己的替代方法将是具有每个SKTGraphic子类的知识和如何呈现它的视图类。在这种情况下,添加一个新的SKTGraphic子类将需要编辑视图类(添加一个新的子句到if / else或switch语句,可能)。根据当前的设计,可以添加一个SKTGraphic子类,而不需要对视图类进行任何更改以使其工作。


I am a little confused as to the definition of classes as Models or Views in the Sketch example AppKit application (found at /Developer/Examples/AppKit/Sketch). The classes SKTRectangle, SKTCircle etc. are considered Model classes but they have drawing code.

I am under the impression that Models should be free of any view/drawing code.

Can someone clarify this?

Thanks.

解决方案

The developer of Sketch outlines his/her rationale a bit in the ReadMe file:


Model-View-Controller Design

The Model layer of Sketch is mainly the SKTGraphic class and its subclasses. A Sketch Document is made up of a list of SKTGraphics. SKTGraphics are mainly data-bearing classes. Each graphic keeps all the information required to represent whatever kind of graphic it is. The SKTGraphic class defines a set of primitive methods for modifying a graphic and some of the subclasses add new primitives of their own. The SKTGraphic class also defines some extended methods for modifying a graphic which are implemented in terms of the primitives.

The SKTGraphic class defines a set of methods that allow it to draw itself. While this may not strictly seem like it should be part of the model, keep in mind that what we are modelling is a collection of visual objects. Even though a SKTGraphic knows how to render itself within a view, it is not a view itself.


I don't know if that is a satisfying answer for you or not. My personal experience with MVC is that while separation of model, view and controller is a "good thing" oftentimes in practice the lines between layers become blurred. I think compromises of design are frequently made for convenience.

For Sketch in particular, it makes sense to me that the model knows how to draw itself inside a view. The alternative to having each SKTGraphic subclass knowing how to draw itself would be to have a view class with knowledge of each SKTGraphic subclass and how to render it. In this case, adding a new SKTGraphic subclass would require editing the view class (adding a new clause to an if/else or switch statement, likely). WIth the current design, an SKTGraphic subclass can be added with no changes required to the view classes to get things working.

这篇关于在Sketch示例AppKit应用程序中的类如何分为Models / Views / Controllers?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 20:47