Modular design patterns for non-object oriented code?

I am looking for an article or other articles on the modular design form. Unfortunately, I did not fully read the article before I lost it, so it may be somewhat vague, but I will try to be as specific as I can be, and explain what I'm trying to do so that someone can suggest other articles .

The article (blog) talked about a programmer who in his experience said that the best way to develop software was in the form of a modular or component approach, so that different components could be selected for different versions of the same or similar software.

The main thing that I remember is a diagram showing different programs on one axis and different components on the other and showing how you can select specific components for certain programs.

The reason I'm looking for this is because I am dealing with some non-object oriented PHP code that is too large to redefine object oriented. This program displays various branded web applications with different but similar rules for each site. Currently, most of the code is as follows:

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

If conditions can be repeated in several places in the code to include different files.

And I would like to review some of them to be more like:

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

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

Any suggestions or tips on how to do this would be appreciated, but I'm not quite sure how to research this for code other than OOP.

Thanks.

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

+1
php design-patterns
source share
2 answers

I understand that spending time on OOP may not help with existing code. In fact, if you are not going to rewrite all existing code, the suggestions are as follows:

  • Make sure that there is only one entry point on the site, if not refactoring.
  • Extract all duplicate functions from the module files into this single script entry: it can be global resource initialization, common tasks, query filtering, layout initialization, etc.
  • Create some procedure that will separate the "modules" depending on the state of the application and the request - the router :) Basically it is a switch or if ... elseif ... else
  • Try to save the "module" files that execute only business logic and assign values ​​to the template.

You may need to split or merge some modules depending on logical fragmentation in an existing source. I once did a similar job when an ugly customer e-commerce site needed to be modified. Fragmented code is best.

+1
source share

I know that you want to do this without OOP, but what you describe is literally OOP. You simply call it “modules”, which we call “objects”, the same.

What you should do is take some time, studying the principles of OOP and how to implement them in PHP. When this happens, you will begin to see the benefits of using objects and classes.

0
source share

All Articles