我什么时候应该扩展一个类,什么时候不应该扩展一个类,它对于 MVC 是否有效?
我正在考虑使用类扩展作为将模型与控制器连接的方式。我尝试在互联网上查找,但找不到有关此主题的任何信息。这让我想到了何时应该延长课程以及出于什么原因延长课程的问题。
这是我的计划:
- 模型类
- 控制器扩展模型
- 新控制器();
- 新视图(控制器);
原因: 我可以将视图不应触及或更改的所有方法和变量设置为受保护(即 protected var myVar:String)。这使我能够确保视图仍然可以访问它所需的数据,但无法进行意外更改。
整个思考过程源于这样一个事实:我不希望我的视图产生任何影响,同时仍然保持独立(即我可以拥有同一模型的多个视图,而不必告诉控制器已添加其他视图) )。
总结:
- 什么时候应该延长课程?什么时候应该避免?
- 我的计划是 MVC 的有效实现吗?
- 有没有更好的方法来满足我的要求来断开视图?
感谢您阅读到最后。
I am considering using class extension as a way to connect my model with my controller. I tried looking on the internet but could not find any information on this topic. This led me to the question of when a class should be extended and for what reasons.
This is my plan:
- model class
- controller extends model
- new controller();
- new view(controller);
Reason:
I can make all methods and variables that the view should not touch or alter protected (i.e. protected var myVar:String). This enables me to ensure that the view still has access to the data it needs but is unable to make accidental changes.
This whole thought process derived from the fact that I don't want my view to have any influence whatsoever, while still remaining independent (i.e. I can have multiple views of the same model without having to tell the controller that an additional view has been added).
To summarize:
- When should a class be extended? When should it be avoided?
- Is my plan a valid implementation of MVC?
- Is there a better way to disconnect the view in a way that meets my demands?
Thank you for reading till the end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
控制器不应该扩展模型 - 它们在 MVC 三元组中执行两个独立的操作,因此应该是两个不同的类。扩展 Model 类的一个有效原因是向其添加额外的功能,例如 BigModel
这里是 MVC 结构各部分的摘要
The controller shouldn't extend the model - they do two separate things in the MVC triad and therefore should be two different classes. A valid reason to extend the Model class would be to add an extra feature to it, for example BigModel
Heres a summary of each part of MVC structure
您的视图将无权访问模型/控制器的受保护方法。受保护并不意味着只读,它意味着只有扩展基类的类才能访问受保护的属性或方法。
要在模型中拥有只读属性,您应该考虑使用私有/受保护的属性,然后为每个属性创建一个公共 getter 函数(然后可以读取属性,但不能设置属性)。
另外,要从视图访问模型,请考虑将模型创建为单例,以便可以从应用程序中的任何位置访问它。
控制器通常除了侦听和分派事件/通知之外不会做太多其他事情,有时对于小型项目,您可以使模型类(单例)扩展 EventDispatcher 并让它几乎完成您想要的所有操作,但这不是纯粹的 MVC,如果项目范围扩大,可能很快会导致技术债务。
Your view will not have access to the protected methods of the model/controller. Protected does not mean read only, it means that only classes that extend the base class can access the protected properties or methods.
To have read only attributes in your model you should look at using private/protected properties and then creating a public getter function for each property (Property can then be read but not set).
Also to have access to the model from the view consider creating the Model as a Singleton so it can be accessed from anywhere in your application.
The controller dosen't usually do much else than listen for and dispatch events/notifications, sometimes for small projects you can make your Model class (Singleton) extend EventDispatcher and have it pretty much do everything you want, but this is not pure MVC and can quickly lead to technical debt if the project scope grows.