Home arrow PHP arrow PHP Composite View Design Pattern: Introducing the Key Concepts

PHP Composite View Design Pattern: Introducing the Key Concepts

The Composite View design pattern offers users the versatility of handling single objects and collections through the same interface. But some programmers hesitate to use it due to its tricky implementation. Hesitate no longer; this multi-part series will take you step by step through some of the best ways to use the Composite View design pattern, complete with code samples to show you the way.

TABLE OF CONTENTS:
  1. PHP Composite View Design Pattern: Introducing the Key Concepts
  2. Defining an abstract composite view class
  3. Defining methods for adding and removing views
  4. Finish sample class: add a method for rendering view templates
By: Alejandro Gervasio
Rating: starstarstarstarstar / 3
August 17, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Regardless of their specific implementation, which may vary from a programming language to another (and even across different applications), one additional advantage that design patterns offer is that their names give you a pretty clear idea of what they can be used for. Good examples of this can be found when building Singletons, Factories, Facades and so forth -- it’s easy to guess what kind of problem they’re designed to tackle, even without seeing how they’re concretely applied.

This concept holds true for many other popular patterns, including the always venerable “Composite.” As you may know, the Composite pattern can be used to handle single objects and collections of them (also known as composites) through the same interface. While the functionality of Composite permits you to perform the aforementioned task in a elegant and efficient way, and it’s also a pristine example of favoring Composition over Inheritance, its implementation is very often confusing.

The good news about Composite is that its underlying logic is incredibly simple to grasp, and it’s present nearly everywhere. Want a concrete example that sheds some light on the topic? Well, say that you’re building an application comprised of a bunch of classes that extend a single abstract parent. To get the application up and running, each class must be bootstrapped individually via a hypothetical method called “bootstrap().” Still with me? Great.

Since you’re a clever programmer who likes to abstract things away, you build the classes in such a way that they can be bootstrapped separately or in conjunction. In doing so, client code calling the “bootstrap()” method could bootstrap a single class, or a collection of them, behind the scenes. Isn’t that nice? Well, feel free to congratulate yourself, because you’ve just implemented a Composite!

What’s more, a similar approach can be followed when working with views, something called in programming jargon the “Composite View” pattern. However, when used specifically in PHP, it allows you to render one or multiple view templates with a single method call, thus making it easy to work with “partials” (coined in the RoR world) and master layouts at the same time.

Considering the flexibility that the Composite View pattern offers to PHP developers, in this article series I’m going to demonstrate how to implement it via some approachable examples. You'll be able to use them as standalone modules or add them to your existing MVC layer. Let’s get started!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: