|
Post by account_disabled on Jan 28, 2024 10:49:55 GMT
设计模式经常被纳入流行的框架中。例如,模型-视图-控制器 (MVC) 设计模式是一种普遍存在的设计模式。在 JavaScript 中,很难将框架与设计模式解耦。通常,特定的框架会对此设计模式有自己的解释。框架伴随着观点,每个观点都迫使你以某种方式思考。 现代框架规定了 MVC 模式的具体实现是什么样子的。当所有解释都不同时,这会令人困惑,从而增加噪音和混乱。当任何代码库采用多个框架时,都会造成令人沮丧的混乱。我心中的问题是,有更好的方法吗? MVC 模式适用于客户端框架,但现代框架发生了变化。今天的现代事物会随着时间的流逝而消亡。在这方面,我想探索替代方案,看看一点点纪律会把我们带向何方。 MVC 模式本身可以追溯到几十年前。这使得它成为一种值得投入编程技能的良好设计模式。MVC 模式是一种可以独立存在的设计模式。问题是,这能让我们走多远? 等等,这是另一个框架吗? Learn to Code with JavaScript 首先,我想消除这个常见的误解:设计模式不是框架。设计模式是解决代码问题的严格方法。 有一定的技能水平是必要的,并且责任由程序员承担。设计模式分离关注点并促进干净的代码。 框架是不同的,因为它不必遵守任何设计模式。区分框架和模式的一种方法是寻找好莱坞原则。好莱坞的原则是:“不要打电话给我们,我们会打电话给你。” 任何时候只要有一个依赖关系决定你何时使用它,它就是一个框架。框架很像好莱坞,因为你无法决定要做什么或如何做。事实上,开发人员就像演员,因为他们在被要求表演时遵循剧本。 避免 WhatsApp 号码数据 使用客户端框架有充分的理由: 框架增加了解决方案的复杂性和风险 您遇到依赖项锁定,这会导致代码无法维护 随着新的流行框架的出现,很难重写现有的遗留代码 研究项目。对于前端图形用户界面来说,这种模式经受住了时间的考验。该模式来自桌面应用程序,但已被证明对网络应用程序也有效。 MVC 设计模式的关键在于清晰地分离关注点。我们的想法是使解决方案易于理解且有吸引力。任何想要进行特定更改的程序员同事都可以轻松找到正确的位置。 企鹅演示 企鹅们!可爱又可爱,是地球上毛茸茸的动物之一。太可爱了,事实上,有 17 种不同种类的企鹅,它们并不都生活在南极洲。 是时候演示企鹅了!我将展示一个在单个页面上显示多个物种的套牌。为此,我想使用 MVC 设计模式和一些规则。我将使用极限编程方法通过单元测试来解决手头的问题,不废话。最后,您应该能够浏览几只企鹅,每只企鹅都有自己的数据和个人资料图片。 Learn to Code with JavaScript 读完本示例后,您应该已经了解了如何在纯 JavaScript 中使用 MVC 设计模式。该模式本身是超级可测试的,所以期待良好的单元测试。 出于跨浏览器兼容性的原因,我将在本演示中坚持使用 ES5。在这种长期设计模式中使用经过验证的语言功能是有意义的。 你准备好了吗?让我们来看看吧。 骨架 该演示将由三个主要部分组成:控制器、视图和模型。每个人都有自己的担忧和需要解决的问题。 其外观如下: 企鹅视觉演示 处理PenguinController事件并且是视图和模型之间的中介。它计算出当用户执行操作(例如,单击按钮或按下某个键)时会发生什么。
|
|