非面向对象代码的模块化设计模式? [英] Modular design patterns for non-object oriented code?

查看:144
本文介绍了非面向对象代码的模块化设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找关于模块化设计形式的文章或其他文章。不幸的是,我没有完全阅读文章之前,我失去了它,所以这可能是一个模糊的,但我会尽可能的具体,因为我可以解释我正在努力做的事情,有人可能会建议其他文章



有关文章(博客文章)是由程序员在他的经验中表示,设计软件的最佳方式是采用模块化或基于组件的方法,可以为相同或类似软件的不同版本选择不同的组件。



我记得的主要事情是显示一个轴上的不同程序和另一个轴上的不同组件的图,并显示如何为特定程序选择特定组件。 p>

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

  if($ _ SESSION ['country'] ===加拿大&& ; $ _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代码。



谢谢。



编辑:我在OOP受过教育,不是我的问题。

解决方案

我明白,与OOP花费的时间可能无法帮助现有的代码。实际上,如果你不重写所有现有的代码,建议如下:


  1. 确保该站点只有一个入口点,如果没有 - 重构。

  2. 将所有重复功能从模块文件解压缩到单个条目脚本中:它可以是全局资源的初始化,常用任务,请求过滤,布局初始化等。

  3. 创建一些例程,根据应用程序状态和请求分配模块 - 路由器:)基本上是 switch if ... elseif ... else

  4. 尝试保持模块文件仅执行业务逻辑并将值分配给模板。

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


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.

解决方案

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.

这篇关于非面向对象代码的模块化设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆