Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > Programming Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
Managing Code Updates Efficiently - The Facade Pattern
Guest190829
Posts: n/a

 

Show Printable Version Email this Page Subscription
Guest190829 01-02-2008, 10:00 PM


Skill Level:
Intermediate-Advanced knowledge in programming; basic understanding of OO structure and concepts in PHP.

** This article may not be reproduced without the owner's (Danny.VBT) permission.


Introduction


Hypothetically, let's imagine that vBulletin just released version 4.0. They announce that the entire back-end architecture has changed and now you must update your quite large and complex modification to support their new code changes because your users (clients) are demanding functionality for vB 4.0

This definitely means hours of inspecting and altering thousands of lines of code, right?

Not if you use implement the Facade pattern!

The Facade Pattern? What?s that?

The Facade Pattern is a design pattern that introduces a whole lot of benefits when dealing with third party code that you have no control over. In short, it acts as a tier between your modification and vBulletin.

Now, you may be asking: But my modification is designed specifically for vBuleltin, why would I want to separate them?

Reasons for Utilizing the Facade Pattern
  • Encapsulation - encapsulating essentially hides an implementation of a certain method from client code.
Take the following example: I want to determine if a user has permission to do a certain action. Normally, you would simply have:
PHP Code:
          
if(!($permissions['myhackpermissions'] & $vbulletin->bf_ugp['myhackpermissions']['candosomething']))
{
    
print_no_permission();

If wrapped in a facade class, you would have something like:

PHP Code:
if(!$this->bridge->permission(?candosomething?))
  {
             
// User doesn't have permission
  

See how simple that looks?
The actual implementation of the permission checking would be in the facade class, but it is only the interface that matters. Now instead of sprinkling vBulletin version specific code throughout your system, you are only providing an interface to that specific implementation.

So what happens when vBulletin releases a new major version and the implementation changes? That brings us to our next reason.
  • Inheritance - Inheritance provides a way to avoid code duplication and get the most of encapsulation.
Let us go back to the previous permission example. Here is the implementation of the permission method:
PHP Code:
class facade 
 
{
           function 
facade()
           {
                
// Some required initialization for vBulletin backend and global variables.
           
}
           function 
permission($option)
           {
                  
// Ternary for readability purposes only...
                  
return ($permissions[PRODUCT_ID . ?permissions?] & $vbulletin->bf_ugp[PRODUCT_ID . ?permissions?][$option]) ? true false;

           }
   } 
This class is a simple facade with a permission method to encapsulate the data. Now pretend vBulletin 4.0 is released and the entire permission handling is now altered. Instead of having to go through all the hassle of finding and editing the version specific code throughout your modification, you can simple utilize inheritance to update the methods that need to be altered.
PHP Code:
class facade_40 extends facade 
{
      function 
permission($option)
       {
                
// 4.0 specific permission handling
       
}


Then at the start of the client code you can determine which class to create:

PHP Code:
if ( //vBulletin version == 4.0 )
{
    
$this->bridge = new facade_40()
}
else {
     
$this->bridge = new facade();
}

// Code code code.....

if($this->bridge->permission('candosomething'))
{
    
//More code 

The example above would normally be a little more dynamic. Listed in the suggested readings below, PHP 5 Objects, Design and Practice list some patterns that help with object generation.

The point of the example is that the code above does not care if $this->bridge is facade or facade_40...the code will continue to function as long as the interface remains. This is an integral concept of OO programming called Polymorphism.

When an upgrade of vBulletin requires that you alter your code, you no longer have to update every single file with the new implementation: Just extend your facade class and add support for proper object generation based on vBulletin version; ultimately saving time from tedious code alterations when you could be adding new, exciting features to your modification.

Implementing a Facade

Utilizing the facade pattern is not always the best choice. If you are only create a small modification with only a few files or whatnot, it may not be worth the time to design your application around a facade. However, if your modification has more than a handful of files with thousands if not hundred of thousands lines of code, the facade pattern may be a huge time saver for you in the future.
  • Global Variables - Due to the architecture of vBulletin, with a facade type implementation, you may have to globalize some of the default variables that run across vBulletin such as $vbulletin, $phrase, $style, $permissions ...etc...etc...
This type of initialization of global variables will most likely be done within the constructor or initialization function.

As with any global variables, you must take good note not to override any essential data.
  • Object Oriented Approach - The Facade pattern requires the use of classes and objects to gain the benefits of encapsulation, inheritance, and composition.
Implementing a facade would require more than one class, so it important that you fully understand OO before implementing any type of design pattern. Do not just use OO because it seems popular to do.
Conclusion

I left this article abstract for a reason. I don?t want to provide code to mimic or to copy and paste, but to allow those who unsure or unfamiliar with object oriented programming to see the benefits of OO even with just vBulletin modifications. Take the time to research and understand Object Orientation and it will be extremely beneficial to you in the future.

Suggested Readings:


PHP 5 Objects, Designs, and Practice by Matt Zandstra; Apress Publishing.

The Object Oriented Thought Process by Matt Weisfeld; Sams Publishing.

The Pragmatic Programmer by Andrew Hunt and David Thomas.

By: Danny Cassidy
Reply With Quote
  #2  
Old 01-04-2008, 05:25 AM
basketmen's Avatar
basketmen basketmen is offline
 
Join Date: Nov 2006
Posts: 446
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
vBulletin just released version 4.0
what? where is it??? i dont get the announcement
Reply With Quote
  #3  
Old 01-04-2008, 05:28 AM
Guest190829
Guest
 
Posts: n/a
Default

It's hypothetical
Reply With Quote
  #4  
Old 01-04-2008, 12:28 PM
bobster65's Avatar
bobster65 bobster65 is offline
 
Join Date: Mar 2006
Location: Montana
Posts: 1,169
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great Article... Thank you Danny!
Reply With Quote
  #5  
Old 01-15-2008, 09:40 PM
alexgeek alexgeek is offline
 
Join Date: Dec 2007
Location: UK
Posts: 128
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by basketmen View Post
what? where is it??? i dont get the announcement
Ha!

Anyway good tutorial. This a nice practical example of OO.
Reply With Quote
  #6  
Old 04-13-2008, 07:45 PM
King Kovifor's Avatar
King Kovifor King Kovifor is offline
 
Join Date: Nov 2004
Location: PA
Posts: 3,872
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

OK. This is a really interesting article.
Reply With Quote
  #7  
Old 04-21-2008, 02:08 PM
dreads dreads is offline
 
Join Date: Feb 2007
Posts: 141
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Very very useful article
Thanks
Reply With Quote
  #8  
Old 05-22-2008, 04:58 PM
Jelmertjee Jelmertjee is offline
 
Join Date: Oct 2006
Posts: 47
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

nice idea, so far my mods have been simple, but I'm already spending time changing stuff for 3.7.. I'll consider this in the future!
Reply With Quote
  #9  
Old 05-25-2008, 03:43 AM
Guest190829
Guest
 
Posts: n/a
Default

Yeah, I would only implement this if your modification is pretty extensive....
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:49 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04575 seconds
  • Memory Usage 2,291KB
  • Queries Executed 22 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (5)bbcode_php
  • (2)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (9)post_thanks_box
  • (9)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (9)post_thanks_postbit_info
  • (8)postbit
  • (6)postbit_onlinestatus
  • (9)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • postbit_imicons
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete