As you probably know, one of the great commandments of good object-oriented programming says that whenever possible, Composition must be used over Inheritance, as the latter is an overrated way of reusing code. While it’s fair to admit that this principle should be used with due caution and, for obvious reasons, applied in the appropriate situations, there’s a case where it really shows all of its splendor. Yes, as the name of this article suggests, this time I’m talking about the implementation of Composite View, a flexible design pattern derived from its cousin Composite. When used for web development, Composite View allows you to manipulate single templates (in most cases in the form of HTML pages) and collections of them via the same interface. Naturally, if you’ve already read all of the preceding tutorials in this series, it’s probable that you now have a clear idea of how to create a few composite view classes with PHP, and how to use them for rendering dynamic web pages with a single method call. I discussed in detail how to accomplish this in the previous installment. Even though rendering several templates through only one class method is by far the major strength of a “typical” Composite View, it’s also possible to use the pattern in a slightly different manner and obtain nearly the same results, at least when creating dynamic web pages. To demonstrate how to create this interesting variation of the pattern, in this last chapter of the series I’m going to utilize the sample classes defined previously to generate a basic (X)HTML document, where each of its sections will be rendered individually before being embedded into a master layout. Want to see how this will be done in a few simple steps? Then start reading right now! A brief look at the composite view classes defined previously As I said in the introduction, it’s feasible to use the composite view classes created in previous chapters of this series to build a dynamic (X)HTML page by using a two-step rendering process. But, before I start demonstrating how to achieve this, I’d like to spend a few moments reintroducing the definitions of the classes. With that said, here’s the abstract parent class, tasked with modeling the structure and behavior of generic view objects: (AbstractView.php)
abstract class AbstractView // constructor // get the specified property from the view // remove the specified property from the view
<?php class ViewException extends Exception{} Since the driving logic of the above abstract parent class was discussed in depth before, I’m not going to waste your time (and mine) explaining again how it works. Instead, I suggest you look at the definitions of the following two classes, which are responsible for handling view objects collectively and individually. Here they are: (View.php)
class View extends AbstractView
(Partial.php)
class Partial extends AbstractView From the above code samples, it’s clear to see how the concrete “View” class is capable of rendering the templates associated with single and multiple view objects through a unique method. Again, this puts in evidence how the Composite View pattern makes a clever use of Composition, instead of relying heavily on the benefits provided by Inheritance. And now that you've learned how the earlier sample classes do their business, I'm going to show the source code of the autoloader created in the preceding chapter. It can be used for lazy-loading classes via the SPL stack offered natively by PHP. Here’s how the pertinent autoloader was originally created: (Autoloader.php)
class Autoloader
<?php class ClassNotFoundException extends Exception{} Well, you’ll have to agree with me that the implementation of the above “Autoloader” class is very easy to follow, as it simply uses the SPL stack for loading classes on demand without having to use multiple PHP includes. That was simple to code and read, wasn’t it? So far, so good. Now that I've reintroduced all of the sample classes defined so far, it’s time to demonstrate how to use them together in a truly creative way. We're going to create a dynamic web page by using the aforementioned two-step rendering process. That’s the topic that I plan to discuss in the following section, so click on the link that appears below and keep reading.
blog comments powered by Disqus |
|
|
|
|
|
|
|