“每个操作/页面一个类”有任何模式吗?
MVC 是一个非常好的模式,但有时将所有内容都放入 Controller 的方法中确实很无聊。控制器不断增长,需要时间来摆脱数千行代码。有些人强烈建议将尽可能多的内容放入模型中,但我更喜欢保持模型干净(我不将面向控制器的方法放入模型中)。
这个想法是将每个控制器的操作放入自己的类中...
class Post_Add {}
class Post_Remove {}
class Post_View {}
所有代码,这对于我们放入 class Post_Parent
中的所有操作类都是通用的,并将其实例传递给操作构造函数。
所以,调用操作看起来像......
$parent = new Post_Parent();
$action = new Post_Add($parent);
$action->run();
那么,我们有什么?
- 每个动作都在单独的类中,所以 我们可以添加尽可能多的私有方法, 我们想要的变量、常量。
- 所有公共代码都分为 父类(
Post_Parent
)并且可以 可以从操作类访问。它 非常适合组织 ACL 等。-
这个想法值得实施吗?有没有类似的设计模式?
谢谢。
MVC is a really good pattern, but sometimes it is really boring to put everything into Controller's methods. Controller is constantly growing and it takes time to get rid of thousands of code lines. Some people highly recommends to put as much as possible into Model, but I prefer to keep Model clean (I don't put controller oriented methods into Model).
The idea is to put each Controller's action into own class...
class Post_Add {}
class Post_Remove {}
class Post_View {}
All code, which is common for all action classes we're putting into class Post_Parent
and passing it's instance into action constructor.
So, calling action will look like...
$parent = new Post_Parent();
$action = new Post_Add($parent);
$action->run();
So, what we have?
- Each action is in separated class, so
we can add as much private methods,
vars, constants as we want. - All common code is separated into
parent class (Post_Parent
) and can
be accessed from action classes. It
is very good for organizing ACL etc.-
Is this idea worth living? Is there any similar design patterns for this?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就我个人而言,我认为您所描述的模式从长远来看不会对您有好处。如果您的控制器已经有“数千行代码”,那么您就会遇到封装的普遍问题,并且为每个操作创建一个类只会将您的问题转移到不同的层。
您的控制器应该薄。您通过撰写帖子已经认识到了这一点。控制器应该协调视图和模型之间的交互。模型是您的业务逻辑所在的地方,因此您的控制器应该只具有足够的逻辑来确保执行适当的验证、调用正确的业务逻辑,并在业务逻辑处理完成时返回正确的视图。
Personally I don't think the pattern you describe is going to serve you well in the long run. If your controllers already have "thousands of code lines" you've got a general problem of encapsulation, and creating a class per action is just going to shift your problem into a different layer.
Your controllers should be thin. You have already recognised this by writing your post. A controller should orchestrate the interaction between your views and your model. The model is where your business logic lives, so your controller should only have enough logic to ensure that the appropriate validation is carried out, that the correct business logic is invoked, and to return the right views when business logic processing has completed.
查看交易脚本和PageController 模式。事务脚本是最基本的领域逻辑模式,适合小型应用程序。 PageController 的用途是处理来自 UI 的输入。如果您希望这是一个命令,那也没关系。您可以执行
PostAddTransactionScript 然后将 $postData 写入数据库或执行任何它应该执行的操作。上面的简化示例仍然符合 MVC,因为它将模型逻辑保留在事务脚本内,并将输入处理保留在表示层内。
是否将输入处理逻辑组织到单个控制器类或许多较小的命令中取决于您。对职责进行分组更有意义,特别是当您需要在命令之间共享状态或通用功能时。
至于你的例子,我宁愿使用 策略模式 并让 Post_Parent 使用命令而不是使用父级的命令,例如
无论如何,我同意其他人的观点,即您的控制器应该很薄,并且模型应该完成主要工作。
Have a look at the Transaction Script and PageController patterns. Transaction script is the most basic of the Domain Logic patterns and suited for small applications. A PageController's purpose is to handle input from your UI. If you want that to be a single command, that's okay. You could do
PostAddTransactionScript would then write the $postData to the database or whatever it is supposed to do. The simplified example above would still be in accordance to MVC because it keeps the Model logic inside the transaction script and the input handling inside the presentation layer.
Whether you organize your input handling logic into a single Controller class or many smaller commands is up to you. Grouping responsibilities just makes more sense, especially if you need to share state or common functionality between the Commands.
As for your example, I'd rather use the Strategy Pattern and have the Post_Parent use a Command instead of the command using the Parent, e.g.
In any case, I agree with the others that your controllers should be thin and the model should do the main work.
我建议稍微后退一下,思考一下为什么控制器增长得这么快。也许您可以进行重构并将一些共享组件提取到单独的模块中?也许您的逻辑中有一些代码应该位于您的模型或视图中?你使用好的模板系统吗?
将控制器分成更小的部分并不能解决根本问题,只能将其隐藏起来。
I'd suggest to back up a little and think why are the controllers growing so much. Maybe you could do a refactoring and extract some shared components into separate modules? Maybe you have some code in your logic that should be in your model or view instead? Do you use a good template system?
Splitting controllers into smaller pieces will not solve the underlying problem, only sweep it under the rug.