Modular design patterns for non-object oriented code?

纵然是瞬间 提交于 2019-12-24 09:28:20

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8931441/modular-design-patterns-for-non-object-oriented-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!