I want to build myself a web application in an object oriented manner.
I know how to build it without object orientation, but am very new to OO, so am unsure whether it should be all OO or only some, and how the objects should be related (or not!). I suppose my question is, "How should I architect it for OO?"
Here is the background:
-----------------------
The application has these basic requirements:
-security
(user validation, session tracking/validation, etc.)
(examined/used once per page hit)
-state
(persistence: session info, cached data)
(examined/used several-many times per page hit)
-control
(something has to be in control of program flow---it may be a single sript, 'main.cgi', or it may be several '<page>.cgi' scripts, depending on how the architecture works out)
(the big cheese, controls everything)
-backend
(methods to store and retrieve information in a database---I'm using DBI, DBD::mysql, mysql)
(examined/used one to several times per page hit)
-common page elements
(elements common to all pages{head, header, nav bars, footer}, generates html, determined in part by variables from the page-specific module(passed in this case)/object(passed or stored, not sure yet), as well as by data stored in the state object)
(used a few to several times per page hit)
-page-specific elements
(elements specific to a single page{the actual 'content'}, generates html from data retrieved from backend or from cached data in the state object)
(used once per page hit)
My current design calls for about 35 actual pages (all dynamic).
The pages fit into several categories or "main pages", such as:
"things related to the home page" (such as 'the home page', 'contact us' and 'about us'),
"things related to the help page" (such as 'the help page', 'support FAQ', 'support search', etc.), and so on.
This leads me to want to limit the number of "url" pages to just the "main pages": 'home', 'help', 'signup', etc., and to have the "sub pages" be specified in the urls by an "action" parameter: 'the home page', 'contact us', 'about us' actions for the "home" page, for example.
Some of my thoughts:
--------------------
For the pages, I can see a heirarchy like page::<main_page>::<sub_page> which seems perfect for object orientation. Therefore, I assume they should be object'ified'.
I can also see that I want a 'state' object so I can easily access its data as needed.
Since I'll be using CGI.pm, I'll also have a $cgi object, and a $dbh object for the database handle. I realize that these two are sort of 'helper' objects that my program will use as needed, and the same is true for my state object.
It is not as clear to me whether the security module should be an object since it will only be used once and not store any data. As an object, it would only have methods, so I suppose it would be a 'helper' class.
My architectural quandary:
--------------------------
In a non-object oriented approach, I understand how I could have either:
-a single program with modules for security, state, common_page_elements, <main_page>, <sub_page>, utilities (such as logging), etc.,
-or-
-one program per 'main page' with a specific module to handle the <sub_page> for each and a set of common modules for security, state, common_page_elements, etc.
When I try to grok this in OO terms, I can't quite figure out how to handle the second approach, "one program per main page". Specifically, if I want to have a 'sub_page' object inheriting from the '<main_page>' class, which in turn inherits from the 'page' class (and possibly inheriting from some generic base class), I don't understand how to have the 'main page' script handle both the program flow (I suppose I could have a 'control' module or object common to all the main pages) 'and' be an object itself!
Oh! I just had a thought (and yes, it was painful, but less so than previous thoughts!)...
..., what about this:
-<main_page>.pl is implemented as a "shell" that uses a control flow module/object (or perhaps a method in the 'page' class) 'and' <main_page>.pm contains the OO stuff to make a page::<main_page>::<sub_page> object (or, perhaps 'sub_page' becomes just a method of <main_page>? I had forgotten that a class is implemented in a package, which I generally associate with a module. Am I on the right track here or barking at the moon?
My request:
-----------
Any thoughts or suggestions on this? Particularly on which portions of the application you would use as classes/objects or not and how you would or would not relate the various classes to each other?
Any insights will be greatly appreciated by this OO newbie!
"Peace, love, and Perl...well, okay, mostly just Perl!" --me
Apprentice