非面向对象代码的模块化设计模式?

发布于 2024-12-28 07:29:39 字数 986 浏览 0 评论 0原文

我正在寻找一篇或其他有关模块化设计形式的文章。不幸的是,我在丢失之前没有完全阅读这篇文章,所以这可能有点含糊,但我会尽力具体并解释我正在尝试做什么,以便有人可以推荐其他文章。

这篇文章(博客文章)是由一位程序员撰写的,他说根据他的经验,设计软件的最佳方法是采用模块化或基于组件的方法,以便可以为相同或相似软件的不同版本选择不同的组件。

我从中记住的主要内容是一张图表,显示一个轴上的不同程序和另一轴上的不同组件,并显示如何为特定程序选择特定组件。

我寻找这个的原因是我正在处理一些非面向对象的 PHP 代码,这些代码太大而无法修改为面向对象。该程序显示不同品牌的 Web 应用程序,每个站点具有不同但相似的规则。目前,大部分代码如下所示:

if($_SESSION['country'] === "Canada" && $_SESSION['brand'] === "X" 
   && $_SESSION['type'] !== "1" || $_SESSION['brand'] !== "Y" 
   && $_SESSION['type'] === "2") {
      include("mod_something.php");
}

其中条件可能会在代码中的多个位置重复以包含不同的文件。

我想修改其中的一些内容,使其看起来更像:

if($_SESSION['component-red'] === true && $_SESSION['country'] === "Canada") {
    include("mod_something.php");
} 

并根据品牌和用户选择将组件变量设置在中心位置(网站使用前端加载或 FrontController 设计模式)。

任何关于如何实现这一点的建议或提示将不胜感激,但我不完全确定如何针对非 OOP 代码进行研究。

谢谢。

编辑:我接受过面向对象编程的教育,但这不是我的问题。

I am looking for an article or other articles about a form of modular design. Unfortunately I didn't fully read the article before I lost it so this might be kind of vague but I will try to be as specific as I can be and explain what I am trying to do so that someone might be able to suggest other articles.

The article (blog post) in question was by a programmer who said in his experience the best way to design software was in a sort of modular or component based approach so that different components could be selected for different versions of the same or similar software.

The main thing I remember from it was a diagram showing different programs on one axis and different components on another and showing how you could select specific components for specific programs.

The reason I am looking for this is that I am dealing with some non-object oriented PHP code which is too large to revise to object oriented. This program displays different branded web applications with different but similar rules for each site. Currently much of the code looks like:

if($_SESSION['country'] === "Canada" && $_SESSION['brand'] === "X" 
   && $_SESSION['type'] !== "1" || $_SESSION['brand'] !== "Y" 
   && $_SESSION['type'] === "2") {
      include("mod_something.php");
}

Where the conditions might be repeated in several places in the code for including different files.

And I would like to revise some of it to look more like:

if($_SESSION['component-red'] === true && $_SESSION['country'] === "Canada") {
    include("mod_something.php");
} 

and have the component variables set in a central location based on brand and user choices (the sites use a front loading or FrontController design pattern).

Any suggestions or tips about how to accomplish this would be appreciated, but I am not entirely sure how to research this for non-OOP code.

Thank you.

Edit: I was educated in OOP but that is not my question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2025-01-04 07:29:39

我知道,花时间研究 OOP 可能对现有代码没有帮助。实际上,如果您不打算重写所有现有代码,建议如下:

  1. 确保站点只有一个入口点,如果没有 - 重构。
  2. 将“模块”文件中的所有重复功能提取到单个入口脚本中:它可以是全局资源的初始化、常见任务、请求过滤、布局初始化等。
  3. 创建一些例程,根据应用程序状态和请求分支“模块” - router :) 基本上它是 switchif ... elseif ... else
  4. 尝试保留“模块”文件仅执行业务逻辑并将值分配给模板。

也许您需要根据现有源代码中的逻辑碎片来拆分或合并一些模块。有一次,当客户的丑陋电子商务网站需要修改时,我也做过类似的工作。最好是完全碎片化的代码。

I understand, that spending time with OOP may not help with existing code. Actually, if you are not going to rewrite all that existing code, suggestions are as following:

  1. Ensure that site has only one entry point, if not - refactor.
  2. Extract all repeating functionality from "module" files into that single entry script: it can be initialization of global resources, common tasks, request filtering, layout initialization, etc.
  3. Create some routine which would branch "modules" depending on application state and request - router :) Basically it is switch or if ... elseif ... else
  4. Try keep "module" files doing only business logic and assigning values to template.

Maybe you will need to split or merge some modules depending on logic fragmentation in existing source. Once I'd done similar job when ugly e-commerce site of customer was in need of modification. It is better that fully fragmented code.

§对你不离不弃 2025-01-04 07:29:39

我知道你想在没有 OOP 的情况下完成它,但你所描述的实际上是 OOP。你只需称其为“模块”,我们称其为“对象”,一样的。

您应该做的是花一些时间学习 OOP 原理以及如何在 PHP 上实现它们。当这种情况发生时,您将开始看到使用对象和类的优点。

I know you want to accomplish it without OOP, but what you describe is literally OOP. You just call it "modules" we call it "objects", same same.

What you should do, is spend some time learning the OOP principles and how to implement them on PHP. When that'll happen, you'll start seeing the advantages of using objects and classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文