Although I must admit that the factory pattern can be used with a great variety of real-world applications, such as form and web page generators, database abstraction layers, and so forth, in this case I'm going to demonstrate how to utilize its neat functionality to build different types of content boxes. These boxes can be quickly included into any web document by simply invoking the appropriate factory class.
Basically, the visual appearance that I plan to achieve with these content boxes is illustrated by the picture below:
As you can see, the above image shows pretty clearly how distinct kinds of web page content boxes can be easily created by using the factory pattern, in this way demonstrating yet another useful implementation for this popular pattern.
All right, now that you have seen how the pertinent content boxes are going to look when included into a specific web page, let me show you the respective definition of the factory class that builds them. Here is the signature for the class in question; examine its source code, please:
See how easy it is to define a basic factory class for building three different types of content boxes? I bet you do! The "ContentBoxFactory" class has been provided with the capacity for returning to client code, a decent variety of content box objects, identified as "GreyContentBox," "BlueContentBox" and "YellowContentBox" respectively.
Obviously, the main difference between the aforementioned content box objects rests upon their background and title colors, something that can be perfectly seen in the image shown a few lines above.
Okay, at this point you hopefully grasped the logic that drives the previous factory class, which indeed presents a simple signature. However, since the prior content boxes need to be included into a sample web document to appreciate their real usefulness, I'm going to build a web page generator class. Its primary function will be creating the CSS styles and web page sections required for using respective content boxes.
Having said that, here is the corresponding definition for this web page generator:
// define 'WebPage' class
As demonstrated previously, the above "WebPage" generator class presents a few basic methods for creating the different sections of a basic web document. It also generates, via the corresponding constructor, the set of CSS styles required to build all of the content boxes that you saw before.
So far, so good right? You have learned how to build two complementary classes, that is the factory and the web page generator respectively, aimed at displaying these simple content boxes on the browser. Nonetheless, there are a few missing pieces in this schema, since the classes that render these boxes remain undefined as of yet.
Now, bearing in mind this issue, in the following section I'm going to show you the complete signatures for these brand new classes, so you can understand more easily how they can be linked with each other.
To learn how these brand new classes will be built, please click on the link that appears below and keep reading.
blog comments powered by Disqus