简体   繁体   English

模型视图控制器(MVC)设计模式 - 如何将多个视图链接到多个模型?

[英]Model View Controller (MVC) Design Pattern- How to link multiple Views to multiple Models?

Example scenario: 5 views on the screen that each increment through the colors of the rainbow by one color each time you press it. 示例场景:屏幕上有5个视图,每次按下它时,每个视图都会通过彩色颜色增加一种颜色。

In keeping with the MVC design, it seems to encourage having a model that is an array of ints or something, and every time a view is pressed it tells the controller "hey, I'm pressed, just fyi" and then have the controller say "ok, I will increment your corresponding spot in the array by one" and then have the model say "I'm changed, whoever cares" and then have the view say "I care, so I will change my color now". 为了与MVC设计保持一致,它似乎鼓励拥有一个int或其他类型的模型,每次按下一个视图时它会告诉控制器“嘿,我被按下,只是fyi”然后有控制器说“好吧,我会把数组中相应的位置增加一个”,然后让模型说“我改变了,无论谁关心”,然后让视图说“我关心,所以我现在就改变我的颜色”。

^That seems absolutely absurd to me. ^这对我来说似乎绝对荒谬。 I'm thinking I must have the way MVC should work completely skewed, as it seems to make much more sense to simply store the data in the button itself. 我想我必须有MVC完全倾斜的方式,因为将数据简单地存储在按钮本身似乎更有意义。 Sure, maybe the functionality of the button will change or be reused so leave what to do when the button is pressed up to its delegate (the controller), but this seems a bit much. 当然,也许按钮的功能会改变或重复使用,所以当按钮被按下到它的代表(控制器)时,不要做什么,但这似乎有点多。

Also, is it then recommended to store an ID with the view? 此外,是否建议在视图中存储ID? How else will the delegate know which one was pressed? 代表怎么知道哪一个被按下了? Then a corresponding ID should be saved with the model? 然后应该与模型一起保存相应的ID? This is starting to remind me of spaghetti-like mysql tables... 这开始让我想起像意大利面条一样的mysql表......

Anyways, just want to make sure I have that correct. 无论如何,只是想确保我有正确的。

ps- I am aware that there is no other worldly force out there that MANDATES I use MVC absolutely perfectly every time, but would still like to know what is considered proper in this scenario :) ps-我知道没有其他世俗的力量可以让我每次绝对完美地使用MVC,但仍然想知道在这种情况下认为什么是正确的:)

In limiting cases investing in program structure may seem like overkill. 在极限情况下,投资计划结构似乎有点矫枉过正。 Would we apply design patterns to a "Hello World" program? 我们会将设计模式应用于“Hello World”计划吗? Do we need to add comments to it? 我们需要添加评论吗? There are no "Best Practices", there are just "Appropriate Practices in this situation". 没有“最佳实践”,只有“在这种情况下的适当做法”。

Your set up is a minimalist system with a trivial model and trivial relationships - hence MVC could be overkill. 你的设置是一个极简主义的系统,有一个简单的模型和琐碎的关系 - 因此MVC可能有点过分。 The particular features of the application: 该应用程序的特殊功能:

  1. There is no interesting model. 没有有趣的模型。 Incrementing one value has no effect on any other value. 增加一个值对任何其他值都没有影响。
  2. There is no interesting control requirement. 没有有趣的控制要求。 Pressing one button does not cause any state change other than to the immediate button. 按下一个按钮不会导致任何状态更改,而不是立即按钮。
  3. This sounds like a throw-away program with no future maintenance requirements. 这听起来像一个扔掉的程序,没有未来的维护要求。

Now let's think about one possible change to the application: it's persistent. 现在让我们考虑应用程序的一个可能的变化:它是持久的。 When you run it tomorrow it restores the state from a database, each time you click a button you need to save the state. 当你明天运行它时,它会从数据库中恢复状态,每次单击需要保存状态的按钮时。

How would you you implement that in your minimalist solution? 您如何在极简主义解决方案中实现这一目标? Now having a common model that knows how to persist itself starts to have value. 现在拥有一个知道如何坚持自己的共同模型开始具有价值。 I'd claim that the MVC structure exactly avoids this turning into spaghetti. 我声称MVC结构完全避免了这种变成意大利面。 It imposes structure, and that structure is a widely used structure that a maintainer would recognised. 它强加了结构,并且该结构是维护者认可的广泛使用的结构。

I may be reading too much into your question, but it sounds a bit like you are finding it troublesome to navigate an existing MVC application. 我可能正在阅读你的问题,但听起来有点像你发现导航现有的MVC应用程序很麻烦。 It's a similar reaction to that which I've seen when someone used to writing small programs hits either a structured or OO program: they get frustrated because there is no single flow to follow, you can't readily see the overall structure. 这与我曾经看到的,当有人习惯于编写小程序时遇到结构化或OO程序的情况类似:他们感到沮丧,因为没有单一的流程可以遵循,你不能轻易看到整体结构。 One thing we need to learn to do is to be able to take a Black-Box approach to code. 我们需要学习的一件事是能够采用黑盒方法来编写代码。 Focus on one this (eg. Controller) and temporarily treat Model and View as a Black_Box. 专注于一个(例如控制器)并暂时将模型和视图视为Black_Box。 I find myself almost "gear-shifting" as I move from one aspect to another of a large system. 当我从大型系统的一个方面移动到另一个方面时,我发现自己几乎“换档”了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM